Disch: It actually is storing Products and not Product*s. I didn't realise this inhibited polymorphism |
It does. Allow me to try to explain:
Since
Customizable Customisable derives from Product, this means that it automatically has
everything Product has,
and a little bit extra that Product does not have. For example:
1 2 3 4 5 6 7 8 9
|
struct Parent
{
int a;
};
struct Child : public Parent
{
int b;
};
|
Here, Child has both a variable named 'a' (which it inherits from Parent), and it also has a variable named 'b' (which Parent does not have). On the other hand, Parent only has an 'a', and it does not have a 'b'.
You can explore this further like so:
1 2
|
cout << sizeof(Parent) << endl; // will print 4 (probably)
cout << sizeof(Child) << endl; // will print 8 (probably)
|
Now what happens if you have something like the below:
1 2 3 4 5
|
Child c; // a Child, 8 bytes in size
Parent p; // a Parent, 4 bytes in size
p = c; // <- !
|
That last line can't copy the full Child to 'p'. It can copy only the data that will fit in 'p'. Since p doesn't have a 'b', c.b can't be copied to p.b (because p.b doesn't exist).
So basically, if you have a Parent, then you only have a Parent. You do not have a Child.
Pointers, on the other hand, are different, because they do not need to store the whole object. The object is typically stored anonymously on the heap and the pointer simply points to it.
Therefore if you have a Parent*, it might be pointing to a Parent, but it could also be pointing to something larger than a Parent (like a Child).
Hopefully that makes sense =x