CARRAY COpy constructor

I

Ishira

Hello,

Please help.

I am seriously at my wits end. Just when I feel I have completely
understood copy contructor, I am stumped yet again.

I have a class which has a CARRAY of objects. I am trying to write a
copy constructor for the class and it's simply havoc! The array
variable is private to the class and simply contains objects without
dynamic variables.So no issue there. Just the very copying of CArray
is giving me problems.

Maybe some code on how you would handle it will help trmedously.

Kindly throw some light on the matter.

Thank you.
Ishi
 
D

David White

Ishira said:
Hello,

Please help.

I am seriously at my wits end. Just when I feel I have completely
understood copy contructor, I am stumped yet again.

I have a class which has a CARRAY of objects. I am trying to write a
copy constructor for the class and it's simply havoc! The array
variable is private to the class and simply contains objects without
dynamic variables.So no issue there. Just the very copying of CArray
is giving me problems.

Maybe some code on how you would handle it will help trmedously.

First of all, only you can tell us what a CArray is, since it is not a
standard type. Also, you have not given any idea of what trouble you are
having.

Whatever CArray it is, suppose your class is called SomeClass and your
CArray member is called mArray, is there a reason that this won't work?

SomeClass::SomeClass(const SomeClass &c)
: mArray(c.mArray), // more copying
{}

DW
 
J

John Harrison

Ishira said:
Hello,

Please help.

I am seriously at my wits end. Just when I feel I have completely
understood copy contructor, I am stumped yet again.

I have a class which has a CARRAY of objects. I am trying to write a
copy constructor for the class and it's simply havoc! The array
variable is private to the class and simply contains objects without
dynamic variables.So no issue there. Just the very copying of CArray
is giving me problems.

Maybe some code on how you would handle it will help trmedously.

Kindly throw some light on the matter.

Thank you.
Ishi

You should be using std::vector not CArray. At least you should if you want
to post to this group, since std::vector is a standard part of the C++
language, and CArray only exists in MFC. std::vector is also much better
designed than CArray, there is no problem copying std::vector objects.

#include <vector>

class B
{
...
};

class A
{
private:
std::vector<B> vec;
};

class A does not need a copy constructor, std::vector is well enough
designed that A will copy correctly with the compiler generated copy
constructor. That is not something that is true of CArray (I believe).

Time to learn some real C++, and leave MFC collection classes behind.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top