Hi,
The book says:
To set the initial size of a CArray, you call the SetSize() function. That in turns calls ConstructElements(), which allocates memory for the number of elements, but does not call a constructor for your object class. You need to supply your own version of ConstructElements() if your objects contain dynamically allocated data members.
1)How does ConstructElements() allocate memory without calling the object's constructor? Does it use sizeof(class_name)?
2)Why can you store an object, but if you want to store an object with a dynamically allocated data member, you have to do something extra ?
3)How would a version of ConstructElements be implemented for a simple object like this(assuming CArray is declared: CArray<A, A&> aArray):
Code:
class A
{
public:
A(int* p)
{
pint = new int;
*pint = *p;
}
private:
int* pint;
};