Solved

CArray of CArray copy constructor needed

Posted on 2009-05-20
7
635 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 114
viewing source code from eclipse 13 110
tripleUp challenge 7 86
how to use laptop or pad camera in vb.net windows application 2 86
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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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