Solved

CArray of CArray copy constructor needed

Posted on 2009-05-20
7
605 Views
Last Modified: 2013-11-20
Hi Experts,
I don't get why I need a copy constructor for a string compare.
The class is of type CArray, and has a CArray member. Which leads to a CArray of a CArray. That might be a problem ?

char sz[] = "1234";
if(strcmp(m_arr.GetAt(i).m_szName, sz) == 0) // error C2558
{ ...
}

Can you please tell me what to do ?

CArray<CMyClass, CMyClass&> m_arr;

class CMyClass

{

public:

	CMyClass(void)

	{

		m_szName[0] = '\0';

	};

	~CMyClass(void) {};

	char m_szName[12];

	CArray<CMySubClass, CMySubClass&> m_sub; 

};

class CMySubClass

{

public:

                CMySubClass(void)

	{

		m_szName[0] = '\0';

	};

	~CMySubClass(void) {};

                char m_szName[12];

};

Open in new window

0
Comment
Question by:stev75
  • 3
  • 3
7 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 24431525
Hi stev75,

I tested the code with both VC++ 6.0 and VS 2008. With VC++ 6.0 the error occurs, with VS 2008 it doesn't.

The difference is that 'old' CArray::GetAt() returns a 'TYPE' instead of 'TYPE&' - so a copy of the element is created and returned.

You can avoid this by using 'CArray::ElementAt()' instead, this returns a reference instead of a copy.

Hope that helps,

ZOPPO
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 24431551
While I'm not sure of exactly what is going on, I'm thinking the issue is that you've declared that your array is going to return references to the object in the array.  I'm thinking the creation of the reference is what needs the copy constructor.

A fix is to change the array to return pointers to the objects in the array.

Rather than ...
CArray<CMyClass, CMyClass&> m_arr;
... instead use...
CArray<CMyClass, CMyClass*> m_arr;

Then, rather than...
if(strcmp(m_arr.GetAt(i).m_szName, sz) == 0)
... instead use...
if(strcmp(m_arr.GetAt(i)->m_szName, sz) == 0)



0
 
LVL 31

Expert Comment

by:Zoppo
ID: 24431575
> I'm thinking the creation of the reference is what needs the copy constructor.
Sorry, but it's exactly the other way round - returning a reference doesn't need to create a copy. The problem is the GetAt doesn't return a reference ...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:stev75
ID: 24431815
thanks, ElementAt() works.
But I have to go the way for a copy constructor, when I want to use
m_arr.Add(myClassObj) That is what the compiler tells me> Again> C2558
It would be nice if you guys can spend a comment on creating a copy constructor, if there's special advise with CArray needed.
Thanks
0
 

Author Comment

by:stev75
ID: 24432006
btw, copy constructor not needed, when you go like this>
instead of
CArray<CMySubClass, CMySubClass&> m_sub;
use the member as a ptr>
CArray<CMySubClass, CMySubClass&>* m_p_sub;  
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 125 total points
ID: 24432085
Hi again,

just add this code to CMyClass:

...
>             CMyClass( const CMyClass& src ) { *this = src; }
>            CMyClass& operator = ( const CMyClass& src )
>            {
>                  if ( this != &src )
>                  {
>                        strcpy( m_szName, src.m_szName );
>                        m_sub.Copy( src.m_sub );
>                  }
>                  return *this;
>            }
...

Using a pointer IMO is not a good idea here. First you have to handle creation/deletion of the pointed array, second you won't create a copy of the m_sub array, you just create a copy of the pointer, so the copy of CMyClass points to the same array. This can produce bad problems.


BTW: Instead of 'ElementAt' you can even use the []-operator (which itself calls 'ElementAt') somehow like this:

> if(strcmp(m_arr[0].m_szName, sz) == 0)


ZOPPO
0
 

Author Comment

by:stev75
ID: 24432295
Good tips. It als now compiles fine. Thanks very much!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to calculate times for developing software? 8 65
Separate into files by filename 12 75
iSeries DB2 Query 2 90
method notes when mouse over in eclipse 5 82
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now