0

Copy Constructor is not Invoked.

#include using namespace std;

class Sample
{
    int x;
    int y;

public:
    Sample(int a, int b)
    {
        x = a, y = b;
    }
    Sample(Sample &S)
    {
        cout << "Copy Constructor Invoked" << endl;
        x = S.x;
        y = S.y;
    }
};

Sample fun(int a, int b)
{
    Sample temp(a, b);
    return temp;
}

int main()
{
    Sample S = fun(1, 2);
}

I'm Returning the Object from the function 'fun'. it creates the object and return the copy of the object. so it needs to call the copy constructor. But Copy Constructor in Not Invoked.The Output of the Program is Empty

surya
  • 5
  • 3

0 Answers0