Consider:
class A
{
public:
virtual void update() = 0;
}
class B : public A
{
public:
void update() { /* stuff goes in here... */ }
Given all the answers that are already here, I think I must be crazy, but this seems right to me so I'm posting it anyways. When I first saw your code example, I thought you were slicing the instances of B
and C
, but then I looked a little closer. I'm now reasonably sure your example won't compile at all, but I don't have a compiler on this box to test.
A * array = new A[1000];
array[0] = new B();
array[1] = new C();
To me, this looks like the first line allocates an array of 1000 A
. The subsequent two lines operate on the first and second elements of that array, respectively, which are instances of A
, not pointers to A
. Thus you cannot assign a pointer to A
to those elements (and new B()
returns such a pointer). The types are not the same, thus it should fail at compile time (unless A
has an assignment operator that takes an A*
, in which case it will do whatever you told it to do).
So, am I entirely off base? I look forward to finding out what I missed.