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 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. 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. 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
by: Jesper |
last post by:
Hi,
Does the concept "copy constructor" from c++ excist in
c#. What is the syntax.
best regards Jesper.
|
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, ...
|
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)
{}
| |
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...
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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 ...
|
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...
| |