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

When to define copy/assgnment constructors

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.