In c++ if I define a class to have a default constructor as well as a parameter based constructor but in the main I only instantiate an instance of the class using the parameter based constructor, what does c++ do ?
Just curious as to what would happen and can't find an answer anywhere.
#include <iostream>
usingnamespace std;
class T
{
public:
T() { cout << "I am constructed by the default constructor.\n"; }
T(int x) { cout << "I am constructed by the parameter based constructor.\n"; }
};
int main()
{
T t1; // object of type T declared, no match with any defined constructors,
// use the default constructor.
T t2{ 5 }; // c++ will call the parameter based constructor.
system("PAUSE"); // wait for the user to press enter
}
If you compile and execute that, you will see that c++ will call the constructor based on your declaration.