answersLogoWhite

0


Best Answer

A copy constructor is a separate, compiler created constructor for a class. This allows the programmer to instantiate a class and copy the contents of the given class into the new class. For example:

If I have a class foobar:

Foobar foobar;

Foobar foobar1( foobar );

The second line creates a class in the exact image of the original class, with all of its variables set the same way as the original class.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

A copy constructor is a special constructor that constructs a new object from an existing object of the same type, thus creating a copy. It works in a similar manner to the following:

int x = 42;

int y = x;

Here, y is an exact copy of x -- they both have the same value, but they are completely independent of one another.

Copy construction typically takes place when passing objects into functions by value, or returning values from functions. For instance:

int square (int x) { return x*x; }

Here, x is a copy of whatever integer we pass to the function, thus x is copy-constructed. The return value is also copy constructed.

Built-in types such as int are not implemented as objects, of course, so they have no constructors. However, when we define our own classes we invariably need to provide a similar means of copying objects. That is the role of the copy constructor.

Even if you do not define a copy constructor, one is generated for you by the compiler. The default behaviour of the compiler-generated copy constructor is to perform a member-wise copy of the class members. In most cases this is fine, however if your class contains a pointer to a resource you will want to override the default behaviour because a member-wise copy would simply copy the pointer (a shallow copy), not what it points at (a deep copy).

For instance, the following class declares a pointer member:

struct foo

{

int* p;

foo (): p {new int{42}} {}

~foo () { delete p; }

};

Let's create an instance of foo:

foo a;

Now consider what happens when we copy that instance:

foo b = a;

assert (a.p b.p);

Again, the compiler-generated version of the copy assignment would result in both objects pointing at the same memory through member-wise copy. Worse still, it would also create a memory leak because the original memory pointed to by b.p was not released. So we must define our own copy assignment operator to perform a deep copy:

struct foo

{

int* p;

foo (): p {new int{42}} {}

foo (const foo& f): p {new int {*f.p}} {} // copy constructor

foo& operator= (const foo& f) {*p = f.p; return *this; } // copy assign

~foo () { delete p; }

};

As well as copy construct and assign, we should also cater for move construct and assign:

struct foo

{

int* p;

foo (): p {new int{42}} {}

foo (const foo& f): p {new int {*f.p}} {} // copy constructor

foo& operator= (const foo& f) {*p = f.p; return *this; } // copy assign

foo (foo&& f): p {f.p}} {f.p=nullptr;} // move constructor

foo& operator= (foo&& f) { int* t = p; p = f.p; f.p=t; return *this; } // move assign

~foo () { delete p; }

};

Note that with move constructor, we take ownership of the incoming memory and nullify the incoming pointer. With move assign we simply swap the pointers. This is because when a move is invoked, we must take ownership of the incoming resources whilst leaving the incoming object in a state that allows it to be destroyed.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A copy constructor is a special constructor that constructs a new object from an existing object of the same type, thus creating a copy. It works in a similar manner to the following:

int x = 42;

int y = x;


Here, y is an exact copy of x -- they both have the same value, but they are completely independent of one another.


Copy construction typically takes place when passing objects into functions by value, or returning values from functions. For instance:


int square (int x) { return x*x; }


Here, x is a copy of whatever integer we pass to the function, thus x is copy-constructed. The return value is also copy constructed.


Built-in types such as int are not implemented as objects, of course, so they have no constructors. However, when we define our own classes we invariably need to provide a similar means of copying objects. That is the role of the copy constructor.


Even if you do not define a copy constructor, one is generated for you by the compiler. The default behaviour of the compiler-generated copy constructor is to perform a member-wise copy of the class members. In most cases this is fine, however if your class contains a pointer to a resource you will want to override the default behaviour because a member-wise copy would simply copy the pointer (a shallow copy), not what it points at (a deep copy).


For instance, the following class declares a pointer member:


struct foo

{

int* p;

foo (): p {new int{42}} {}

~foo () { delete p; }

};


Let's create an instance of foo:


foo a;


Now consider what happens when we copy that instance:


foo b = a;

assert (a.p b.p);


Again, the compiler-generated version of the copy assignment would result in both objects pointing at the same memory through member-wise copy. Worse still, it would also create a memory leak because the original memory pointed to by b.p was not released. So we must define our own copy assignment operator to perform a deep copy:


struct foo

{

int* p;

foo (): p {new int{42}} {}

foo (const foo& f): p {new int {*f.p}} {} // copy constructor

foo& operator= (const foo& f) {*p = f.p; return *this; } // copy assign

~foo () { delete p; }

};

As well as copy construct and assign, we should also cater for move construct and assign:


struct foo

{

int* p;

foo (): p {new int{42}} {}

foo (const foo& f): p {new int {*f.p}} {} // copy constructor

foo& operator= (const foo& f) {*p = f.p; return *this; } // copy assign

foo (foo&& f): p {f.p}} {f.p=nullptr;} // move constructor

foo& operator= (foo&& f) { int* t = p; p = f.p; f.p=t; return *this; } // move assign

~foo () { delete p; }

};


Note that with move constructor, we take ownership of the incoming memory and nullify the incoming pointer. With move assign we simply swap the pointers. This is because when a move is invoked, we must take ownership of the incoming resources whilst leaving the incoming object in a state that allows it to be destroyed.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is copy construct in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is c plus c plus c plus c plus c?

c + c + c + c + c = 5 * c.


How do you concatinate two files in c plus plus?

Copy the first file then append the second file to the copy.


C program plus copy of string to another?

strcpy


Users can use a shortcut such as CTRL plus C for operations they perform frequently?

CTRL C to copy


What command is the control key plus c?

Copy the selected text (or object).


How do you convert meters to centimeters in c plus plus using the for loop construct?

It is unnecessary to use a for loop to convert meters to centimeters. Just multiply by 0.01.


What is the use of print option in c plus plus?

C++ has no print option. The print option in your IDE allows you to print your C++ source code, thus giving you a "hard" copy of your code.


How is a bitwise copy created in c plus plus?

You could just use memcpy(3), using sizeof() to get the object size.


To Construct An Angle Of 75 Degrees and then construct its copy?

It would take time but if you keep trying hard you will find a way.


Are construct and array equivalent in c?

No.


How do you construct a double clipping circuit?

Just Copy yours nearest friend..


What is a sample code for C plus plus?

Copy and paste this and it will say hello world! #include <iostream> int main() { std: :cout < < "Hello, world!\n ";