c ++向基类的向量指针,以及对多个派生类方法的访问 [英] c++ vector pointer to base class, and access to multiple derived classes methods
问题描述
class A {
public:
int var;
virtual int getVar(){return var; }
};
class B:public A {
public:
int anothervar;
int getAnotherVar(){return anothervar; }
};
类C:public A {
public:
int finalvar;
int getFinalVar(){return finalvar;}
};
int main(){
vector< A *> myvec;
myvec.push_back(new B()); //暗示所有的构造函数都是ok
myvec.push_back(new C());
cout<< myvec [0] - > geVar(); // this works fine
cout<< myvec [0] - > getAnotherVar(); //我该怎么做?
cout<< myvec [1] - > getFinalVar(); //我该怎么做?
return 0;
}
这只是我想要解决的另一个问题的表示。所以我的问题是,如果有可能从基类的指针向量调用派生类方法,因为我不能将它们声明为纯虚拟在基础,因为我不想使用它们在派生。 p>
您可以转换成正确的类型:
向量< A *> myvec; //指针向量到基类
myvec.push_back(new B()); //暗示所有的构造函数都是ok
myvec.push_back(new C());
cout<< myvec [0] - > geVar(); // this works fine
cout<< myvec [1] - > geVar(); // this works fine
B * b = dynamic_cast< B *>(myvec [0]);
assert(nullptr!= b);
cout<< b-> getAnotherVar();
C * c = dynamic_cast< c *>(myvec [1]);
assert(nullptr!= c);
cout<< c-> getFinalVar();
但是,需要以这种不统一的方式访问你的集合可能指向一个更大的类设计问题。
更准确地说,在这里它看起来所有罚款和dandy,因为您在访问它们的同一个地方创建B和C类实例,因此,哪个类的隐含信息在哪个索引是local,但在一个现实的例子中,你可能会创建对象,并在不同的地方访问它们,并依赖于隐藏的索引信息为你想要的非统一访问可能会是一个bug的来源。 >
class A{
public:
int var;
virtual int getVar() { return var; }
};
class B: public A{
public:
int anothervar;
int getAnotherVar() { return anothervar; }
};
class C: public A{
public:
int finalvar;
int getFinalVar() { return finalvar;}
};
int main () {
vector<A*> myvec;
myvec.push_back (new B()); // implying all constructors are ok
myvec.push_back (new C());
cout << myvec[0]->geVar(); // this works fine
cout << myvec[0]->getAnotherVar(); // how can I do this ?
cout << myvec[1]->getFinalVar(); // how can I do this ?
return 0;
}
This is just a representation of another problem I'm trying to solve. So my question is, if it is possible to call derived classes methods, from a pointer vector of a base class since I cannot declare them as pure virtual at the base, because I don't want to use them in both derived.
You can do this by casting to the right type:
vector<A*>myvec; //pointer vector to base class
myvec.push_back(new B()); //implying all constructors are ok
myvec.push_back(new C());
cout << myvec[0]->geVar(); //this works fine
cout << myvec[1]->geVar(); //this works fine
B* b = dynamic_cast<B*>(myvec[0]);
assert(nullptr != b);
cout << b->getAnotherVar();
C* c = dynamic_cast<C*>(myvec[1]);
assert(nullptr != c);
cout << c->getFinalVar();
However, needing to access your collection in this kind of non-uniform manner may point to a bigger class design issue.
More precisely, here it looks all fine and dandy because you create the B and C class instances in the same place where you access them, so the implicit information of which class is at which index is "local", but in a realistic example, you would probably create the objects and access them in different places, and relying on that implicit index information for the non-uniform access you want will probably be a source of bugs later.
这篇关于c ++向基类的向量指针,以及对多个派生类方法的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!