Sign in to follow this  

Copy Constructor

This topic is 2314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It'd be like dividing by zero: It'll explod the entire Universe!!!`11!`!!!eleven

[code]
class Fred {
public:
Fred() {
}

Fred( Fred temporary ) {
}
};

int main() {
Fred fred1;

// In order to copy-construct fred2 from fred1, we will pass fred1 into fred's copy-constructor.
// To pass an object by value requires that we copy-construct a new fred from fred1.
// However, in order to copy-struct a new fred from fred1 by value, we need another fred ...
// And so on ...
Fred fred2(fred1);
}
[/code]

Share this post


Link to post
Share on other sites
Passing by value is better choice in some circumstances. [url="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/"]Want Speed? Pass by Value.[/url]

Share this post


Link to post
Share on other sites
Hidden
[quote name='Ftn' timestamp='1313126747' post='4848111']
Passing by value is better choice in some circumstances. [url="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/"]Want Speed? Pass by Value.[/url]
[/quote]

But not in copy constructors.

Share this post


Link to post
As fastcall22 put rather humorously, there is no way to resolve a copy constructor with the object passed by value.

The reason is that the copy constructor would need to already exist for this to work. The whole purpose of the copy constructor is to create a copy of an object when it's needed. Any time you assign by value, you are invoking the copy constructor, and by passing in by value to a copy constructor, you'd need the copy constructor, which would then need the copy constructor, and so on. In this case it's not a question of efficiency, so much as it's the only way to do it.

Share this post


Link to post
Share on other sites

This topic is 2314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this