Solved

CArray of CArray copy constructor needed

Posted on 2009-05-20
7
663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

623 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