Solved

CArray of CArray copy constructor needed

Posted on 2009-05-20
7
595 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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
> 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:stev75
Comment Utility
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
Comment Utility
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 30

Accepted Solution

by:
Zoppo earned 125 total points
Comment Utility
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
Comment Utility
Good tips. It als now compiles fine. Thanks very much!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

16 Experts available now in Live!

Get 1:1 Help Now