I'm trying to create any iterable to store subclass objects in.
I currently am using a vector of pointers to the superclass, but this doesn't seem to allow any of the subclass-specific declarations to be used.
class piece
{
public:
int foo()
{
return 1;
}
};
class pawn : public piece
{
public:
int foo()
{
return 2;
}
};
std::vector<piece*> pieces;
pieces.push_back(new pawn());
pieces[0]->foo(); // returns 1 though pieces[0] should be a pawn
The call to pieces[0]->foo() returns 1, when I want it to return 2.
Debugging shows that the "new pawn()" creates a piece pointer, so I get why it doesn't use the pawn function, but I don't know why it isn't a pawn in the first place.