I can't reach a method of a sub-class' instance when several conditions are merged :
- There is in the super-class an attribute of type string.
- The instance have been created in a loop
- The instance is stored in a vector that takes super-class pointers
It's so look like this :
class Parent
{
public :
string name;
virtual void myMethod() = 0;
};
class Child : public Parent
{
public :
void myMethod();
};
void Child::myMethod()
{
cout << "I'm a child";
}
int main(void)
{
vector<Parent*> children;
for(unsigned int i = 0 ; i < 1; i++ )
{
Child c;
children.push_back(&c);
}
(*children[0]).myMethod();
}
In that case the code over with an error : "pure virtual method called
terminate called without an active exception". I guess that it's trying to access to 'Parent::myMethod' that is virtual and so fail. To avoid that issue I can :
- Remove the attribute 'name' of the super-class
- Change the type of that attribute (to int for exemple)
- Append the elements to the vector from outside of the for loop.
I just can't figure what is going on in that specific case...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…