【发布时间】:2023-03-07 06:23:01
【问题描述】:
我有简单的示例代码
class Child2{
public:
Child2(){
cout<<"this is child2"<<endl;
}
void test2(){
cout<<"Method Child 2"<<endl;
}
};
class Child1{
public:
Child2 **child2;
Child1(){
child2 = new Child2 *[1];
child2[0] = new Child2();
cout<<"this is child1"<<endl;
}
void test1(){
cout<<"Method Child 1"<<endl;
}
};
class Top{
public:
Child1 **child1;
Top(){
child1 = new Child1 *[2];
child1[0] = new Child1();
child1[1] = new Child1();
cout<<"this is top"<<endl;
}
};
现在,我知道我可以通过使用访问方法“test2()”
top->child1[0]->child2[0]->test2();
但是,如果我想动态访问该方法而不放置数组索引,那么有什么办法可以访问所有“child2”中的“test2()”。我需要保留双指针,因为我想在运行时使用输入分配对象。
谢谢
【问题讨论】:
标签: c++ arrays oop object pointers