问题
class classa {
public:
virtual void foo();
};
class classb : public classa {
public:
virtual void foo() override;
};
void classa::foo()
{
std::cout << "foo from a" << std::endl;
}
void classb::foo()
{
std::cout << "foo from b" << std::endl;
}
int main()
{
std::vector<classa> stuff;
classa a;
classb b;
stuff.push_back(a);
stuff.push_back(b);
stuff[0].foo();
stuff[1].foo();
return 0;
}
I expected the above code to return
foo from a
foo from b
but it returns both as foo from a.
I think this is because the vector stores classa but I am not sure.
How can I get classb:foo() to be called by b?
回答1:
This happens because of object slicing, you'll need to keep a vector of pointers (preferably smart pointers).
I'm assuming stuff is defined as std::vector<classa> stuff;. When you do
stuff.push_back(b);
the object pushed into the vector is a slice of b - particulary the classa part. All other type info is lost. For this to work as expected, you'd need:
std::vector<classa*> stuff;
or similar. The way your code is now, you can't get it to work because stuff[1] is no longer a classb, but a classa.
来源:https://stackoverflow.com/questions/12519286/overriding-parent-classs-function