Sign in to follow this  

[C++] Copy constructor questions

This topic is 4205 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

I'm learning about the copy constructor and I'm having a little difficulty understanding some things. I have three questions for anyone gracious enough to answer: They all have the same question: Why the copy-ctor is not called Using this class: class Bar { public: inline Bar(const Bar& x) { std::cout<<"copy constructor\n"; } inline Bar() { std::cout<<"default constructor\n"; } }; ... Question 1: Please explain to me why the following does not call the copy constructor (aside from calling the default one)?: int main( int argc, char**argv ) { Bar x = Bar(); return 0; } My thoughts were that the copy ctor would also be called because the right operend is returning an newly created object by value and it matches the copy ctors parameter arguments so, this temp value object would be passed by reference, and the left object x would be the this pointer in the copy ctor and thus, print the "copy constructor" string, after printing the "default constructor" string. As a backup to go with the example, My C++ Primer books states the following: "Copy-initialization uses the = symbol" ..."Copy-initialization always involves the copy constructor" Question 2: I have the same question for the following also:
void X(Bar x) { /*...*/ }

int main( int argc, char**argv )
{
	X(Bar());
	return 0;
}


Question 3: Last but not least, return by value does not call my copy ctor:
Bar Y(void)
{
	Bar y;
	return y;
}

int main( int argc, char**argv )
{
	Y();
	return 0;
}


If the object being returned instead is from the functions parameter (and not declared locally), it does call the copy ctor on return. Hmm. As always, Thanks in advance. - xeddiex

Share this post


Link to post
Share on other sites
These are guesses:

1:
This syntax has always seemed to me to simply construct x. Perhaps ctor(a), b=a, dtor(a) gets instantly recognised as replaceable by ctor(b).

2:
Same as 1: The function parameter is b, and the Bar() in the main() function is a.

3:
There is nothing to copy to. If you replaced "Y()" with "Bar z = Y()" it should copy.

Share this post


Link to post
Share on other sites
Quote:
Original post by xeddiex
They all have the same question: Why the copy-ctor is not called


Which compiler, optimized or debug build?

Share this post


Link to post
Share on other sites
Quote:
Original post by Trienco
Quote:
Original post by xeddiex
They all have the same question: Why the copy-ctor is not called


Which compiler, optimized or debug build?


MSVC++ 2005 SE,
Debug build,
Optimization setting: Maximize Speed (/O2)

- xeddiex

Share this post


Link to post
Share on other sites
Quote:
Original post by xeddiex
MSVC++ 2005 SE,
Debug build,
Optimization setting: Maximize Speed (/O2)


Not sure how far optimizing and debug build is working at all, but if you enable any kind of optimizing you should always expect the compiler to not be a complete moron and optimize stuff away when it's completely pointless. Especially avoiding the creation of lots of temporary objects and copy them all over the place when it's not required. On the other hand, one would think that the output alone would prevent the constructor calls from being removed.

Share this post


Link to post
Share on other sites
From the C++ Standard ISO/IEC 14882:2003, section 12.8 paragraph 15:
Quote:

Whenever a temporary class object is copied using a copy constructor, and this object and the copy have the same cv-unqualified type, an implementation is permettied to treat the original and the copy as two different ways of referring to the same object and not perform a copy at all, even if the copy constructor or destructor have side effects.

Emphasis mine.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
From the C++ Standard ISO/IEC 14882:2003, section 12.8 paragraph 15:
Quote:

Whenever a temporary class object is copied using a copy constructor, and this object and the copy have the same cv-unqualified type, an implementation is permettied to treat the original and the copy as two different ways of referring to the same object and not perform a copy at all, even if the copy constructor or destructor have side effects.

Emphasis mine.


Good read. Thanks. However, I have a question concerning that: Temporaries are const unnamed objects-/Yes?. That said, why does the following code require a type-cast to a const qualified object in order for the copy-ctor to be called?:


void X(const Bar x) { /*...*/ }

int main( int argc, char**argv )
{
Bar x = (const Bar)Bar();
// and..
X((const Bar)Bar());
return 0;
}
/* output:
default constructor
copy constructor
default constructor
copy constructor
*/




Aren't temporaries really const?

- xeddiex
[p.s, I really need to get a copy of the standard]

Share this post


Link to post
Share on other sites
Temporaries are not necessarily const. For example this code is perfectly legal:

class Foo {
public:
void non_const_function(void) {}
};

int main(int, char **) {
Foo().non_const_function();
}

Share this post


Link to post
Share on other sites

This topic is 4205 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