可能重复:
在 C++ 中访问类 Veriables
嗨,我在 C++ 中有以下类层次结构
Class1 {
vector<Class2> vecClass2;
}
Class2 {
private:
const Class1 * ptrClass1;
vector<Class3> vecClass3;
public:
Class2(const Class1 * ptrClass1);
int intC2publicVar;
string strC2publicVar;
}
Class3 {
private:
const Class2 * ptrClass2;
vector<Class4> vecClass4;
public:
Class3(const Class2 * ptrClass2);
}
Class4 {
private:
const Class3 * ptrClass3;
vector<Class5> vecClass5;
public:
Class4(const Class3 * ptrClass3);
void class4Method() const;
}
在 class4Method() 我正在做这样的事情:
void Class4::class4Method() const {
const Class2 * pC2 = ptrClass3->ptrClass2;
int valClass2 = pC2->intC2publicVar;
//Above value is giving wrong value, I have no idea from where it is fetching the wrong value
string strVatClass2 = pC2->strC2publicVar;
//Above line of code cause run time termination of code and programs stops as soon as this line executes.
const Class2 c2 = * pC2;
//Above line of code cause run time termination of code and programs stops as soon as this line executes.
}
我不知道为什么这发生在 Class4 的 class4Method() 中。请帮我解决这个问题。我的整个项目都因为这个问题而受到打击,如果不解决它,我就无法继续前进。