el_champo
el_champo

Reputation: 1219

C++ Copy Constructors

I recently wrote a piece of code which did

SomeClass someObject;
mysqlpp::StoreQueryResult result = someObject.getResult();

where SomeClass::getResult() looks like:

mysqlpp::StoreQueryResult SomeClass::getResult()
{
mysqlpp::StoreQueryResult res = ...<something>...;
return res;
}

Now, using the example in the first code snippet, when I compiled and ran, the program crashed with an ABORT signal. I then changed the first snippet to:

SomeClass someObject;
mysqlpp::StoreQueryResult result(someObject.getResult());

which worked fine. Also, just to try it out, I changed it again to:

SomeClass someObject;
mysqlpp::StoreQueryResult result;
result = someObject.getResult();

which also worked fine.

Now, I just can't figure out why the first example failed, and the next two succeeded. As I understand, in the first example, the copy constructor is used to initialise result. But isn't this also the case in the second example? So why did the second example succeed? The 3rd example makes a bit more sense - since the copy const isn't used, we just assign after construction.

In short, what's the difference between:

FooClass a = someObject.someMethodReturningFooClassInstance();

and

FooClass a(someObject.someMethodReturningFooClassInstance());?

Muchos thanks!

Upvotes: 0

Views: 1268

Answers (6)

erikkallen
erikkallen

Reputation: 34391

In the purest theory, the copy constructor should be called in both cases. However, there is something called Return Value Optimization (RVO) which the compiler is allowed to use in these cases. If RVO is used, the copy constructor will not be called. Perhaps your compiler uses RVO in one case and not the other?

Edit: This only applies to the case

SomeClass someObject;
mysqlpp::StoreQueryResult result;
result = someObject.getResult();

Upvotes: 0

Dominik Grabiec
Dominik Grabiec

Reputation: 10655

Strictly speaking in the first case the default constructor is called followed by the assignment operator and in the second case it uses just the copy constructor.

Okay, my initial assumption was wrong, and apparently in both cases just the copy constructor would get called (well in the assignment case an additional "conversion" constructor may be called as well). I'll fire up the compiler after some sleep and verify this in my development environment.

Upvotes: 2

R.Chatsiri
R.Chatsiri

Reputation: 107

I think first case is assigned value(right value) to object.It's send value to object.In 2,3 exmple is Implicit object and Explicit object concept.You not should write code by assigned value to direct object.

Upvotes: 0

Henrik Hartz
Henrik Hartz

Reputation: 3675

Well actually, the first one will do an intermediate copy with operator=, while the second one will do a direct copy construct.

Upvotes: 0

G S
G S

Reputation: 36818

I don't think there's any difference in the two cases. The same copy constructor is called both times.

Are you sure this is exactly what you've written in your code?

Upvotes: 4

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247899

You could just put a breakpoint (or even a printf statement) inside the copy constructor, and you'd know exactly when it was called, you know. SO can't replace basic debugging. ;)

But yes, the copy constructor should be called in the two first cases. The third case uses the assignment operator instead.

Have you tried running it in the debugger? It should break on an ABORT signal.

Upvotes: 1

Related Questions