473,509 Members | 10,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The copy constructor

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
3 1544
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
5721
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
21173
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
8
20017
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
2526
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
4288
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
3587
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
2880
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.