CArray: SetSize()-->ConstructElements()
-
My beginning visual C++ book says that when you declare a CArray, and then call the SetSize() function, the global helper function ConstructElements() is called to allocate memory for the number of elements you want to store. The default version of ConstructElements() sets the contents of the allocated memory to zero and doesn't call a constructor for your object class, so you need to suppy your own version of ConstructElements() if that action isn't appropriate for your objects. That will be the case if data members of objects of your class are allocated dynamically, or if other initialization is required. First of all, if my object class looked like this: (sorry, the code tags are not preserving the indenting)
class A { public: A(int* p) { pint = new int; *pint = *p; } private: int* pint; };
why do I even need to implement ConstructElements()? If I declared a C++ array like this:A MyA_Array[10];
I wouldn't have to do anything special to store an object of class A in the array. So, what's different about how the MFC CArray class allocates memory? Second, how would I implement ConstructElements() for class A? -
My beginning visual C++ book says that when you declare a CArray, and then call the SetSize() function, the global helper function ConstructElements() is called to allocate memory for the number of elements you want to store. The default version of ConstructElements() sets the contents of the allocated memory to zero and doesn't call a constructor for your object class, so you need to suppy your own version of ConstructElements() if that action isn't appropriate for your objects. That will be the case if data members of objects of your class are allocated dynamically, or if other initialization is required. First of all, if my object class looked like this: (sorry, the code tags are not preserving the indenting)
class A { public: A(int* p) { pint = new int; *pint = *p; } private: int* pint; };
why do I even need to implement ConstructElements()? If I declared a C++ array like this:A MyA_Array[10];
I wouldn't have to do anything special to store an object of class A in the array. So, what's different about how the MFC CArray class allocates memory? Second, how would I implement ConstructElements() for class A?Hi, Well the problem is the constructor. If you create an array of objects then the default constructor is called for each object in your array. If you have no default constructor, that is a constructor with no parameters or all parameters set to default values, the construction of the array will fail. I think that's the reason why you need to implement ConstructElements to make sure the right constructor gets called. Regards G. Steudtel
-
Hi, Well the problem is the constructor. If you create an array of objects then the default constructor is called for each object in your array. If you have no default constructor, that is a constructor with no parameters or all parameters set to default values, the construction of the array will fail. I think that's the reason why you need to implement ConstructElements to make sure the right constructor gets called. Regards G. Steudtel
Hi, Thanks for the response. First, I'm confused about how allocating memory for a CArray works, and second I have no idea what I'm supposed to do to store objects with a dynamically allocated data member in a CArray. 1)My book says the default version of ConstructElements() allocates memory for the number of elements you want to store in the array, but it does not call a constructor for your objects. I assume that it must allocate memory using sizeof() then. Now, how come a regular object can be stored in the CArray but you have to do something extra for an object with a dynamically allocated data member? 2) How do you implement ConstructElements() for the simple object with a dynamiclly allocated data member that I listed in my first post?