By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

The copy constructor

P: n/a
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

Apr 21 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
po******@gmail.com wrote:
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.

--
Ian Collins.
Apr 21 '06 #2

P: n/a
po******@gmail.com wrote:
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.

Apr 21 '06 #3

P: n/a

po******@gmail.com wrote:
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.
This is just my intuition but I would appreciate some feedback, thanks.

Cheers,

Paulo Matos


Apr 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.