C++ C++;:从B对象访问a类实例(包含B类对象列表)的数据成员

C++ C++;:从B对象访问a类实例(包含B类对象列表)的数据成员,c++,oop,C++,Oop,考虑以下代码: #include <iostream> #include <map> class Value { public: void set(const int intValue){ intValue_ = intValue; } int read() const { return intValue_; } void replaceIfInMap(){ std::map<int,

考虑以下代码:

#include <iostream>
#include <map>

class Value
{
    public:
        void set(const int intValue){ intValue_ = intValue; }
        int read() const { return intValue_; }
        void replaceIfInMap(){
            std::map<int,int>::iterator it;
            it = valuesToReplace_->find(intValue_);
            if(it != valuesToReplace_->end()){
                intValue_ = it->second;
            }
        }
        Value(std::map<int,int>* valuesToReplace) : valuesToReplace_(valuesToReplace){}
    private:
        std::map<int,int>* valuesToReplace_;
        int intValue_;
};

class Holder    {  
    public:
        void doStuffWithValues(){
            Value a(&valuesToReplace_), b(&valuesToReplace_), c(&valuesToReplace_);
            a.set(1); b.set(2); c.set(3);
            valuesToReplace[2]=5;
            a.replaceIfInMap(); b.replaceIfInMap(); c.replaceIfInMap();
            std::cout << "a: "  << a.read()
                      << " b: " << b.read() 
                      << " c: " << c.read() << std::endl;
        }
    private:
        std::map<int,int> valuesToReplace_;
};

int main()
{
    Holder holder;
    holder.doStuffWithValues();
}
#包括
#包括
阶级价值
{
公众:
无效集(常量int-int-value){int-value\uu=int-value;}
int read()常量{return intValue_;}
void replaceIfInMap(){
std::map::it迭代器;
它=valuesToReplace->find(intValue);
if(it!=valuesToReplace->end()){
intValue=它->秒;
}
}
值(std::map*valuesToReplace):valuesToReplace(valuesToReplace){}
私人:
std::map*valuesToReplace\uUx;
int-int值;
};
类持有者{
公众:
void doStuffWithValues(){
值a(&valuesToReplace)、b(&valuesToReplace)、c(&valuesToReplace);
a、 一套(1);二套(2);三套(3);
valuesToReplace[2]=5;
a、 replaceIfInMap();b.replaceIfInMap();c.replaceIfInMap();
标准::cout
为什么上面的代码还能工作?Holder::valuesToReplace
二等兵

它是私有的,所以Holder::doStuffWithValues可以访问它,因为它是一个成员函数,没有任何错误

Value a(&valuesToReplace_), b(&valuesToReplace_), c(&valuesToReplace_);
            a.set(1); b.set(2); c.set(3);
这里,所有的值对象都有valuesToReplace\指向同一个映射,这就是您想要的吗?看起来很奇怪,我要么有一个静态映射(在赋值时进行复制),要么有一个智能指针来防止意外删除(但允许空值)

我如何才能以更快捷的方式访问valuesToReplace_uuu成员 方便(最好更优雅)的方式


您可以将其保留为私有,并使用公共成员函数返回映射的begin/end const_迭代器,或者使用不依赖于内部实现的setIntForInt/getIntForInt访问器方法。

将对
valuesToReplace
映射的引用传递给
replaceIfInMap
方法如何

class Value
{
    public:
        void set(const int intValue){ intValue_ = intValue; }
        int read() const { return intValue_; }

        void replaceIfInMap(std::map<int,int> const& valuesToReplace_){
            std::map<int,int>::const_iterator it;
            it = valuesToReplace_->find(intValue_);
            if(it != valuesToReplace_->end()){
                intValue_ = it->second;
            }
        }

        Value() {}
    private:
        int intValue_;
};

class Holder    {  
    public:
        void doStuffWithValues(){
            Value a, b, c;
            a.set(1); b.set(2); c.set(3);
            valuesToReplace_[2]=5;
            a.replaceIfInMap(valuesToReplace_);
            b.replaceIfInMap(valuesToReplace_);
            c.replaceIfInMap(valuesToReplace_);
            std::cout << "a: "  << a.read()
                      << " b: " << b.read() 
                      << " c: " << c.read() << std::endl;
        }
    private:
        std::map<int,int> valuesToReplace_;
};
类值
{
公众:
无效集(常量int-int-value){int-value\uu=int-value;}
int read()常量{return intValue_;}
void replaceIfInMap(标准::映射常量和值存储位置){
std::map::const_迭代器it;
它=valuesToReplace->find(intValue);
if(it!=valuesToReplace->end()){
intValue=它->秒;
}
}
值(){}
私人:
int-int值;
};
类持有者{
公众:
void doStuffWithValues(){
a、b、c值;
a、 一套(1);二套(2);三套(3);
valuesToReplace_u[2]=5;
a、 replaceIfInMap(valuesToReplace);
b、 replaceIfInMap(valuesToReplace);
c、 replaceIfInMap(valuesToReplace);

std::cout这个helper类真的有必要吗?看起来您所做的一切可以总结为:
int replace\u maybe(int n){auto it=m.find(n);return it!=m.end()?it->second:n;}