访问一个成员类的指针,该成员类存储指向另一个成员类的指针
Accessing a pointer to a member class which stores a pointer to another member class
我有一个类指向数据存储成员类(Inputs):
class Calc
{
public:
Calc(Inputs *input) : input(input) {}
void performCalc();
private:
Inputs *input;
};
在Inputs类中,我存储了各种数据输入:
class Inputs
{
public:
Inputs(std::string &directory, LogFile &log);
~Inputs();
private:
WriteLogFile &writeToLog;
WeatherData *weather;
EvaporationData *evaporation;
friend class Calc;
}
现在,当我在performCalc()方法中,我不能在输入对象中访问我的天气类,该对象是Calc类的成员,使用指针表法?
input->weather //does not work
点表示法也不起作用(我不认为它会,因为这里没有通过引用传递的链接。)
input.weather //does not work
我错过了什么?
编辑:对不起!我忘了提到Calc类已经是Inputs类的一个friend class
。
您已经将weather
定义为Inputs
的私有成员,因此它对Calc
对象不可见。您有3个选项:
- 将
weather
设置为public。 - 加
Calc
为Inputs
好友 - 为
weather
创建一个getter方法(推荐,因为它改善了封装)
由于'Input'类的'weather'成员在私有字段中,您正试图从Calc类的'performCalc'方法访问,这是Input类之外。我认为这就是以上两种方法都行不通的原因。
将'weather'变量放入Input类的公共字段或尝试下面,使用get()方法访问Input类的私有字段
class Inputs
{
public:
Inputs(std::string &directory, LogFile &log);
~Inputs();
WeatherData* getWeatherData(){
return weather ;
}
private:
WriteLogFile &writeToLog;
WeatherData *weather;
EvaporationData *evaporation;
}
相关文章:
- 如何使用基类指针引用派生类成员
- 在具有全局类型def的类中使用成员函数指针
- 从类指针的向量创建成员变量的向量
- 向量成员在管理类指针C++队列时丢失
- 指向成员的指针对托管类无效
- 类方法返回指向具有模板的类成员的指针
- C++指向派生类成员的指针
- C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板专用化
- 调用C++成员函数指针,而不知道哪个类
- 如果基类指针无法访问派生类成员函数,那么多态性有什么方便的呢?
- C++:私有类指针成员返回未定义的值
- C++基类指针调用子虚函数,为什么基类指针可以看到子类成员
- 如何将不同的成员函数指针分配给注册类的不同实例?
- 从类中提供指向私有成员的指针
- C++指向成员的指针的类内初始化会使 MSVC 失败(但 GCC/Clang 工作)
- 将数据成员的指针传递给基类构造函数是否安全?
- C++ 类指针成员行为奇怪(错误)
- 从成员函数指针类型中删除类指针
- D 指针/pimpl 模式基类指针访问派生类成员
- 您将如何使用标准引号解释指向基类和派生类成员的指针的这种差异?