Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ copy constructor invocation

Tags:

c++

As far as i know, a copy constructor is invoked in the following scenarios :

1) Pass by value
2) Return by value
3) When you create and initialize a new object with an existing object

Here's the program :

#include <iostream> 
using namespace std; 
class Example 
{ 
    public:
        Example() 
        {
            cout << "Default constructor called.\n";
        }
        Example(const Example &ob1) 
        {
            cout << "Copy constructor called.\n";
        }
        Example& operator=(const Example &ob1) 
        {
            cout << "Assignment operator called.\n"; 
            return *this;
        }
        ~Example()
        {
            cout<<"\nDtor invoked"<<endl;
        }
        int aa;
};

Example funct() 
{
    Example ob2;
    ob2.aa=100;
    return ob2;
}



int main() 
{
    Example x;
    cout << "Calling funct..\n";
    x = funct();
    return 0;
}

The output is:

Default constructor called.

Calling funct..

Default constructor called.

Assignment operator called.

Dtor invoked

Dtor invoked

Please correct me, IIRC the following sequence of calls should occur :

1) Constructor of x is called

2) Constructor of ob2 is called

3) The function returns and so copy constructor is invoked (to copy ob2 to unnamed temporary variable i.e funct() )

4) Destructor of ob2 called

5) Assign the unnamed temporary variable to x

6) Destroy temporary variable i.e invoke its destructor

7) Destroy x i.e invoke x's destructor

But then why copy constructor is not invoked and also only 2 calls to dtors are there whereas i expect 3.

I know compiler can do optimizations, however, is my understanding correct ?

Thanks a lot :)

Regards

lali

like image 621
ghayalcoder Avatar asked Nov 23 '09 14:11

ghayalcoder


People also ask

How many times copy constructor will be invoked?

You call the function by value and do two copies inside.

In what way a copy constructor is automatically invoked?

In C++, a Copy Constructor may be called for the following cases: 1) When an object of the class is returned by value. 2) When an object of the class is passed (to a function) by value as an argument. 3) When an object is constructed based on another object of the same class.

What is copy constructor in C?

A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.


2 Answers

A copy constructor might not be invoked when you return by value. Some compilers use return value optimization feature.

Read about "Return Value Optimization"

like image 163
Prasoon Saurav Avatar answered Oct 23 '22 18:10

Prasoon Saurav


The part of the standard which tells you when compilers may elide copies is 12.8/15. It's always up to the compiler whether to do actually perform the elision. There are two legal situations, plus any combination of them:

  • "in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type"

  • "when a temporary class object that has not been bound to a reference would be copied to a class object with the same cv-unqualified type".

The former is usually referred to as the "named return value optimization", and it's what permits the output you're seeing in your example. The latter in effect turns copy-initialization into direct initialization, and could occur for instance if your code did Example x = Example();.

Other copy elisions are not permitted, except of course that the usual "as-if" rules apply. So if the copy constructor has tracing in, then the following code must call it:

Example x;
Example y = x;

But if x were otherwise unused, and the cctor had no side-effects, then I think it could be optimized away, just like any other code that does nothing.

like image 33
Steve Jessop Avatar answered Oct 23 '22 18:10

Steve Jessop