class Motion
{
protected :
/ * Things */
public :
virtualvoid Delete(void);
void foo(void);
/* Things */
};
class DoubleMotion : public Motion
{
public :
void Delete(void);
};
The function 'foo' contains "Delete();" somewhere in the code.
Then I create a few objects:
1 2 3 4 5 6 7 8
deque<Motion> someDeQue;
/* Things, then we enter a for loop */
DoubleMotion* NewMotion = new DoubleMotion(...);
someDeQue.push_back(*NewMotion);
/* This is done several times */
Then I use the function foo like that:
someDeQue[0].foo();
and no matter what I do, it always uses Motion::Delete, instead of DoubleMotion::Delete().
// http://ideone.com/1oWD0K
#include <iostream>
struct base
{
virtualvoid print_type() const {
std::cout << "base\n";
}
};
struct derived : public base
{
void print_type() const {
std::cout << "derived\n";
}
};
int main()
{
base b; // b is an object of type base. It can never be anything else.
b.print_type();
derived d;
d.print_type();
b = d; // If we set b equal to an object of type derived, it is still an object of type base.
b.print_type();
base* ptr = &b; // ptr is a pointer to some class in the base hierarchy
ptr->print_type(); // if we point it a a base class object, we get base class behavior
ptr = &d; // if we point it a derived class object, we get derived class behavior
ptr->print_type();
}
Store pointers in your container (preferably of the smart variety.)