The copy constructor

P

pocmatos

Hi all,

Imagine I have:
class X {
public:
// Some methods...

private:
vector<MyObject*> vec;

};

I think that the default copy constructor will just copy an object of
this class into a new one where vec is new by the pointers inside vec
will point to the exact same place as the original one, right? It'll
not copy MyObject also, right?

If I want this I'll have to implement my own copy constructor... i.e.
I'll have to implement X(X&) and operator= on X, right?

This is just my intuition but I would appreciate some feedback, thanks.

Cheers,

Paulo Matos
 
I

Ian Collins

Hi all,

Imagine I have:
class X {
public:
// Some methods...

private:
vector<MyObject*> vec;

};

I think that the default copy constructor will just copy an object of
this class into a new one where vec is new by the pointers inside vec
will point to the exact same place as the original one, right? It'll
not copy MyObject also, right?

If I want this I'll have to implement my own copy constructor... i.e.
I'll have to implement X(X&) and operator= on X, right?
right.
 
R

Rolf Magnus

Hi all,

Imagine I have:
class X {
public:
// Some methods...

private:
vector<MyObject*> vec;

};

I think that the default copy constructor will just copy an object of
this class into a new one where vec is new by the pointers inside vec
will point to the exact same place as the original one, right? It'll not
copy MyObject also, right?

Yes. The vector and the pointers it contains will be copied. The objects
they point to won't.
If I want this I'll have to implement my own copy constructor... i.e.
I'll have to implement X(X&) and operator= on X, right?

Well, the copy constructor is probably better X(const X&), but yes, you will
need that. You'll probably also need to define a destructor that deletes
the objects. This is called "The Rule of Three". If you need one of a copy
constructor, copy assignment operator and destructor, you probably need all
three of them.
 
F

Fei Liu

Hi all,

Imagine I have:
class X {
public:
// Some methods...

private:
vector<MyObject*> vec;

};

I think that the default copy constructor will just copy an object of
this class into a new one where vec is new by the pointers inside vec
will point to the exact same place as the original one, right? It'll
not copy MyObject also, right?

If I want this I'll have to implement my own copy constructor... i.e.
I'll have to implement X(X&) and operator= on X, right?

If you found you need to implement your own copy constructor, then
follow the rule of 3, also implement your copy assignment operator and
destructor.
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top