1

Hi I have the below code:

#include <iostream>

using namespace std;

class A {
public:
    int p;
    A() {
        cout << "Inside A constr" << endl;
    }
    A(const A& a) {
        cout << "Inside copy constr" << endl;
    }
    A* operator=(const A& b) {
        cout << "Inside assignment oper" << endl;
    }
};

A f1() {
    cout << "Inside f1" << endl;
    A a;
    cout << "Just before return from f1" << endl;
    return a;
}

int main() {

    A x = f1();
    return 0;
}

When I execute the above program I am getting the below output

Inside f1
Inside A constr
Just before return from f1

My doubt is why copy constructor is not called while executing the statement A x = f1();

Surender Panuganti
  • 333
  • 1
  • 5
  • 14
  • 1
    http://en.wikipedia.org/wiki/Return_value_optimization , http://en.wikipedia.org/wiki/Copy_elision – Igor Tandetnik Nov 16 '14 at 16:11
  • 1
    Use `-fno-elide-constructors` option on [_`GCC/Clang`_](http://coliru.stacked-crooked.com/a/3d24f2ca9d7d11b0) to disable copy-elision, and see your copy constructor call – P0W Nov 16 '14 at 16:15

0 Answers0