473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1971
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(void): // default
array(0), size(0) { }
explicit
doubleVector(size_t n): // explicit
array(new double[n]), size(n) { }
//doubleVector(const doubleVector& v): // copy shallow
// array((double*)v), size(v.extent()) { }
doubleVector(const 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
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...
1
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...
5
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...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
13
by: MurphyII | last post by:
Just a little sample : class A { public: A( ) { } template<typename T> A( const typename T& a) {
10
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, ...
4
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...
26
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
by: subramanian | last post by:
Consider the code fragment: class Test { public: Test(const Test &temp); ... }; ....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.