[Solved] Why destructor is being called but construction not being called when passing object as a parameter? [closed]


Passing an instance of a class by value invokes the copy constructor.

The compiler implements the copy constructor by default (essentially a memberwise copy after invoking copy constructors of any base classes) if the class definition does not explicitly supply one. This compiler-generated copy constructor will not call one of the other constructors you have implemented, so constructing the copy will not print anything. However, the destructor will be invoked to clean up the copy when done.

Within your class, if you implement a copy constructor as follows;

Test(const Test &from) : x(from.x)
{
    std::cout << "Copy constructor invoked" << std::endl;
}

you will find that the copy constructor is indeed being invoked.

solved Why destructor is being called but construction not being called when passing object as a parameter? [closed]