Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ConstructElements of a CArray within an another CArray

Status
Not open for further replies.

graetzd

Technical User
Jun 29, 2001
17
US
Hello Forum Gurus:

I am trying to figure out how to implement a CArray of a structure that contains within it another CArray.

The outer structure desired is:

struct BASE
{
int BasePoly;
CArray <int,int> AdjTo;
};

and I can declare a member of this in my document:

class CMainDoc : public CDocument
{
// Attributes
public:
CArray <BASE,BASE> m_RawData;

// Implementation
public:
void ReadListForRecursion();
}

My document member function ReadListForRecursion() is going to read data in and dynamically grow based on the input data. The idea is that for each element of m_RawData I will be reading in a single value to fill up .BasePoly and some number of integers to fill up the CArray member AdjTo that is within m_RawData.

I am new with templates and collections of CObjects so I am having a hard time understanding the MSDN Library help. I think it is telling me that I need to create an overridding function ConstructElements (and DestructElements and a few others) because my initial element (the BASE structure) needs to initialize the AdjTo CArray. The MSDN Library example has something like:

&quot;For example, you might override ConstructElements for an array of CPerson objects as follows:

class CPerson : public CObject { . . . };
CArray< CPerson, CPerson& > personArray;

template <> void AFXAPI ConstructElements <CPerson> ( CPerson* pNewPersons, int nCount )
{
for ( int i = 0; i < nCount; i++, pNewPersons++ )
{
// call CPerson default constructor directly
new( pNewPersons )CPerson;
}
}&quot;

But I don't understand how to implement this with what I have. Where do I put this override? How do I write it with what I have at the start of this posting? etc. etc?

Thank you for any help.

David H. Graetz

 
David, so your code will go something like this:
Code:
void CMainDoc::ReadListForRecursion(){
	// whatever to read the data
	while( .... still have more data){

		BASE b;
		b.BasePoly = whatever;
		while( ... integers for this BASE object are still left)
			b.AdjTo.add( someInt);

		m_RawData.add(b);
	}
}

does that help?
-pete
 
Pete,

Thanks for the try but it's a no-go. I had tried that before and the error message I get is ...&quot;error C2664: 'Add' : cannot convert parameter 1 from 'struct BASE' to 'struct BASE' No copy constructor available for struct 'BASE' &quot;

Does that jog any other ideas?

David G.
 
ooops, my bad.. i totaly missed that
[cannon] ... (pete)

If you change your template definition to this:

CArray <BASE*,BASE*> m_RawData;

you can use heap based BASE structures and bypass the need for a copy constructor/assignment operator combination. The unfortunate side effect is that you then have to manage memory and be sure to clean up you BASE structures that were allocated on the heap.

Another option is to generate a copy constructor and assignment operator for the BASE structure that performs a deep copy of the CArray elements. Now this would not be an effiecient approach but it may be fine for your application. You can use the CArray::Copy() function to handle the deep copy for you.

hope that helps
-pete

 
Pete,

Thank you - your first suggestion of changing my template defintion to take a reference worked perfectly!

David G.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top