Solved

CArray of CArray copy constructor needed

Posted on 2009-05-20
7
619 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

775 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