I'm trying to have a vector of pointers to vectors of differents classes and where each classes is derived from a same base class.
My code:
#include <iostream>
#include <vector>
#include <stdlib.h>
class A
{
public:
A() { std::cout << "A constructor.\n"; }
virtual ~A() { std::cout << "A destructor\n"; }
virtual void iAm() { std::cout << "I am A.\n"; }
};
class B : public A
{
public:
B() { std::cout << "B constructor.\n"; }
~B() { std::cout << "B destructor.\n"; }
virtual void iAm() { std::cout << "I am B.\n"; }
private:
std::string s;
};
class C : public A
{
public:
C() { std::cout << "C constructor.\n"; }
~C() { std::cout << "C destructor.\n"; }
virtual void iAm() { std::cout << "I am C.\n"; }
private:
std::string s;
int n;
};
int main()
{
std::vector<std::vector<A>*> vect;
vect.resize(3);
vect[0]=new std::vector<A>;
vect[1]=(std::vector<A>*) new std::vector<B>;
vect[2]=(std::vector<A>*) new std::vector<C>;
vect[0]->push_back(A());
vect[0]->push_back(A());
vect[1]->push_back(B(methods are A methods));
vect[1]->push_back(B());
vect[2]->push_back(C());
vect[2]->push_back(C());
(*vect[0])[0].iAm();
(*vect[0])[1].iAm();
(*vect[1])[0].iAm();
(*vect[1])[1].iAm();
(*vect[2])[0].iAm();
(*vect[2])[1].iAm();
}
But the execution give me:
A constructor.
A destructor.
A constructor.
A destructor.
A destructor.
A constructor.
B constructor.
B destructor.
A destructor.
A constructor.
B constructor.
A destructor.
B destructor.
A destructor.
A constructor.
C constructor.
C destructor.
A destructor.
A constructor.
C constructor.
A destructor.
C destructor.
A destructor.
I am A.
I am A.
I am A.
I am A.
I am A.
I am A.
I don't understand why, although I create B
and C
objects, the call of the method iAm()
call the A's
iAm()
. The call of B
and C
iAm()
must call the versions of B
and C
because the constructor are B
and C
and because I just cast pointers to the vectors, not the elements in the vector.
What I didn't understand about this?
Thanks You.