473,811 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When to define copy/assgnment constructors

My understanding about defining your own copy and assignment constructors is
whenever there is a member of pointer type. Is this true or is there any
exception to this"rule"?

How about when you define a class which doesn't have a pointer type variable
as member and this class could be derived, and a new member of pointer type
could be added in the future? Does this mean that all classes that will be
inherited in the future should be defined with its copy and assignment
constructor?

Is it right to say that either compiler-generated or programmer-provided
copy and assignment constructors will do member-wise object copying or
assignment all the time?

Is there any place in C++ that copying between objects could be bit-by-bit?

Thanks!
Jul 22 '05 #1
2 2037
Birt wrote:
My understanding about defining your own copy and assignment constructors is
whenever there is a member of pointer type. Is this true or is there any
exception to this"rule"?
Define it whenever you need one. Define it whenever the default copy
operation isn't good enough for one of your members. Sometimes it's
perfectly valid to do a shallow copy of pointers, sometimes not. You
have to think about it for each class you write, in the context of the
class' purpose. There aren't any hard rules except common sense things
like "don't do a shallow copy of an object with pointers *if* both
objects will try to deallocate the pointed-to memory".

How about when you define a class which doesn't have a pointer type variable
as member and this class could be derived, and a new member of pointer type
could be added in the future? Does this mean that all classes that will be
inherited in the future should be defined with its copy and assignment
constructor?

Is it right to say that either compiler-generated or programmer-provided
copy and assignment constructors will do member-wise object copying or
assignment all the time?
Compiler generated ones, yes. Programmer provided ones can do whatever
the programmer thinks is sensible.

Is there any place in C++ that copying between objects could be bit-by-bit?


Sure, look up memcpy(). Regular assignment of fundamental types already
has this effect for the bits participating in the object's value
representation.
Regards,
Jacques.
Jul 22 '05 #2
Birt wrote:
My understanding about defining your own copy and assignment constructors is
whenever there is a member of pointer type.
Is this true or is there any exception to this"rule"?

How about when you define a class which doesn't have a pointer type variable
as member and this class could be derived,
and a new member of pointer type could be added in the future?
Does this mean that all classes that will be inherited in the future
should be defined with its copy and assignment constructor?

Is it right to say that either compiler-generated or programmer-provided
copy and assignment constructors
will do member-wise object copying or assignment all the time?

Is there any place in C++ that copying between objects could be bit-by-bit?
cat doubleVector.cc

#include <cassert>
#include <cstdlib>

class doubleVector {
private:
// representation
double* array;
size_t size;
public:
// functions
size_t extent(void) const {
return size; }
// operators
operator double*(void) const {
return array; }
operator double*(void) {
return array; }
const
double& operator[](size_t j) const {
return array[j]; }
double& operator[](size_t j) {
return array[j]; }
doubleVector& operator=(const doubleVector& v) {
doubleVector& u = *this;
assert(extent() == v.extent());
for (size_t j = 0; j < extent(); ++j)
u[j] = v[j];
return u;
}
// constructors
doubleVector(vo id): // default
array(0), size(0) { }
explicit
doubleVector(si ze_t n): // explicit
array(new double[n]), size(n) { }
//doubleVector(co nst doubleVector& v): // copy shallow
// array((double*) v), size(v.extent() ) { }
doubleVector(co nst doubleVector& v): // copy deep
array(new double[v.extent()]),
size(v.extent() ) { *this = v; }
};

In this example, an object of type doubleVector contains
a pointer to an array of double precision floating point numbers
and the extent of that array.
The *semantics* of type doubleVector require a "deep" copy
of the right hand side (rhs) into the left hand side (lhs)
and that the extent of the rhs and lhs are equal.
The semantics also require a deep copy constructor.

The default assignment operator and copy constructors
won't do the deep copy. The programmer is obliged to override them
with definitions that do the deep copy.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
5816
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
1
6784
by: Russ Ford | last post by:
Hi all, I'm trying to get inheritance and constructors clear in my head (and in my code). I have the following inheritance situation (all derivations public): A is the base class B is inherited from A C is inherited from B
5
1626
by: Tim Clacy | last post by:
When exiting function scope, which occurs first: a) destruction of local objects b) copy of value for return From disassembly of a debug target, it looks like the return value is copied before local objects are destroyed. Is this standard behaviour? Can the same behaviour be expected for any optimisation level? As an example, in the function 'int A::fn()' here, is 'b' destroyed before 'i' is copied or vice-versa?:
8
20035
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
13
2926
by: MurphyII | last post by:
Just a little sample : class A { public: A( ) { } template<typename T> A( const typename T& a) {
10
2586
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, we do not need to use the copy-constructor. So depending on the above reasoning I can avoid call by value and return by value for class objects, this bypasses the problem or it seems to me like that. Could any one give me some simple examples...
4
5033
by: Ajay | last post by:
Hi all, Can anybody please tell me that what exactly is the need of copy constructor giving a real life example where i can not do things with assgnment operator but can do things i want with copy constructor only.Thanks in advance. ajay
26
15818
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
3
2393
by: subramanian | last post by:
Consider the code fragment: class Test { public: Test(const Test &temp); ... }; ....
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10135
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9205
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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 we have to send another system
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.