0
// Example program
#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
    Test()
    {
        cout << "Default constructor" << endl;
    }

    Test(const Test& ob)
    {
        cout << "lvalue- copy constructor" << endl;
    }

   Test& operator=(const Test& ob)
    {
        cout << "lvalue - assignment" << endl;
        return *this;
    }
};

Test getObj()
{
    Test ob;
    return ob;
}

void f1(const Test& ob)
{
    cout << "f1" << endl;
}

int main()
{
    f1(getObj());
}

Output:
Default constructor
f1

Given that ob in f1() is allocated on the stack, I expected the copy constructor to be called to create a temporary object whose reference is passed to f1(). Looks like it's not the case. Any ideas ?

I have compiled the program with C++98 compliant compiler with all optimizations switched off.

KodeWarrior
  • 3,538
  • 3
  • 26
  • 40

0 Answers0