When you need polymorphism, you need to use either pointers or references. Since containers (or arrays) can't store references, you have to use pointers Essentially change your picture class's vector to: std::vector). The reason for this is because the vector stores its data in an array, which needs to know the size of the objects it's storing.
If the sizes of these objects are different (which they might be for different shapes) then it can't store them in an array If you store them as pointers then they all have the same size ( sizeof(Shape*) ) and also have access to the shape's vtable, which is what allows polymorphic behaviour.
When you need polymorphism, you need to use either pointers or references. Since containers (or arrays) can't store references, you have to use pointers. Essentially change your picture class's vector to: std::vector and appropriately modify the other member functions.
The reason why you can't/shouldn't store them as value types is because vector is a homogenous container i.e. It only stores data of one type (and only one type -- subclasses are not allowed! ).
The reason for this is because the vector stores its data in an array, which needs to know the size of the objects it's storing. If the sizes of these objects are different (which they might be for different shapes) then it can't store them in an array. If you store them as pointers then they all have the same size (sizeof(Shape*)) and also have access to the shape's vtable, which is what allows polymorphic behaviour.
2 With C++0x features like improved smart pointers and move semantics, one could even make a memory-safe version of this (vector >). – bobbymcr Mar 7 '10 at 0:11 If you don't use smart pointers, remember the container won't automatically clean up the pointed-to data. It calls destructors on the pointers - but those destructors don't do anything.
You should add a destructor to your picture class which iterates through the shapes deleting them. Using a smart pointer class, as bobbymcr suggests, avoids this issue. – Steve314 Mar 7 '10 at 0:17 You are the man!
Thank you so much. That was it. – the_gastropod Mar 7 '10 at 0:21.
Use covariant return types. See FAQ 20.8 for your clone methods. You can rely on the factory method as well to create the Shape objects.
Also, you cannot have a container of abstract class objects, abstract classes cannot be instantiated. Instead, create a container of pointers/references to derived concrete objects. Note, if you are using pointer, it becomes your responsibility to clear them.
The container will not de-allocate the memory properly. You can use smart pointers instead of raw pointers to handle this more efficiently. Look up scoped_ptr and shared_ptr from Boost.
On first skim, I saw the "covariant return types" and assumed it was answering the wrong question. Putting that after the main answer may have earned you more upvotes. You got my +1 now though.
– Steve314 Mar 7 '10 at 0:31.
Also, you cannot have a container of abstract class objects, abstract classes cannot be instantiated. Instead, create a container of pointers/references to derived concrete objects. Note, if you are using pointer, it becomes your responsibility to clear them.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.