I've got the following code:
Class A {
public:
A::A(const char* name):
_name(name)
{}
virtual void doSomething();
private:
const char* _name;
}
Class B : public A {
B::B(const char* name):
A(name)
{}
void doSomething() {
//do something
}
}
So far so good, but I've experiencing an error crosses initialization of B* newB
in the following code:
std::vector<A*> vectorOfAs;
switch (enumType) {
case enum1 :
B* newB = new B("foobar");
vectorOfAs.push_back(newB);
break;
case enum2 :
C* newC = new C("barfoo");
vectorOfAs.push_back(newC);
break;
}
Why the error?
Some background:
I want to store pointers of derived classes in a vector for easy searching/iterating. The names are unique so I can iterate through the vector and look for the pointer which points to the object with the right name. When calling a method from a pointed-to object, the inherited one should be called (//do something
).
Edit: @François Andrieux: you're right. made (serious) typo.
Changed Class B : public B
to Class B: public A