2009年8月3日 星期一

Copy constructor and Assignment operator in C++

copy constructor:

A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object.

Normally the compiler automatically creates a copy constructor for each class (known as an implicit copy constructor) but for special cases the programmer creates the copy constructor, known as an explicit copy constructor. In such cases, the compiler doesn't create one.

An explicit copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file, in which case a destructor and an assignment operator should also be written.

The copy constructor will be called under the following cases:
Object A;
Object B(A);
Object B = A;
type function (Object a);
Object a = function();


Under the following case, the assignment operator "=" will be called.

Object A;
Object B;
A = B;

Overloading copy assignment operator "="的時機:

When deep copies of objects have to be made, exception safety should be taken into consideration. One way to achieve this when resource deallocation never fails is:

1. Acquire new resources
2. Release old resources
3. Assign the new resources' handles to the object

copy assignment operator和copy constructor的不同:

The copy assignment operator differs from the copy constructor in that it must clean up the data members of the assignment's target (and correctly handle self-assignment) whereas the copy constructor assigns values to uninitialized data members.


The rule of three in C++:

The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class defines one of the following it should probably explicitly define all three:

* destructor
* copy constructor
* copy assignment operator



#include
#include
class Array
{
public:
int size;
int* data;

explicit Array(int size)
: size(size), data(new int[size]) {}

Array(Array const& copy)
: size(copy.size), data(new int[copy.size]) {
std::cout<<"call copy constructor...."<<"\n";
std::copy(copy.data, copy.data + copy.size, data);
}
Array & operator = (const Array & other){
std::cout<<"call assignment operator...."<<"\n";
if (this != &other) // protect against invalid self-assignment
{
// 1: allocate new memory and copy the elements
int * new_array = new int[other.size];
std::copy(other.data, other.data + other.size, new_array);

// 2: deallocate old memory
delete [] data;

// 3: assign the new memory to the object
data = new_array;
size = other.size;
}
// by convention, always return *this
return *this;
}



~Array()
{
delete[] data;
}
};

int main()
{
Array first(20);
Array sec(20);
first.data[0] = 25;
sec.data[0]=19;

{
Array copy = first;

std::cout << first.data[0] << " " << copy.data[0] << "\n";
copy=sec;
std::cout << sec.data[0] << " " << copy.data[0] << "\n";
sec.data[0]=100;
std::cout << sec.data[0] << " " << copy.data[0] << "\n";
} // (1)

first.data[0] = 10; // (2)
}

referenc:

http://en.wikipedia.org/wiki/Copy_constructor
http://en.wikipedia.org/wiki/Assignment_operator_in_C%2B%2B
http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)

4 則留言:

匿名 提到...

ich weiГџ nicht, dass auch zu sagen levitra generika preisvergleich viagra ohne rezept forum [url=http//t7-isis.org]cialis[/url]

匿名 提到...

Also, the government's humane programs have been stripped to feed the big military equipment. These types of so called psychics are referred to as gypsies in the field because they move from victim to victim. Because of this it will incur you a number of amount for this to move ownership collected from one of party to a different. instant payday loans The main dilemma you need responded is whether the teachers is recognized or not.

匿名 提到...

The lender typically will have the check along with wait until the due date to be able to cash that. You may then repay the bridging bank loan from the profits that will be generated from the profit of your latest home. In this kind of game, you should know and confronting industry leader the financing through subsequent paycheqe. pay day loans online Applying for a less-than-perfect credit unsecured loans which has a virtually any big scars are worth wanting to remove.

匿名 提到...

I was able to find good information from your blog posts.


my webpage :: anti cellulite treatment