If I create my own class, MyClass and then declare it my code as follows:
Is it guaranteed to call its default constructor?Code:MyClass mc;
Or should I declare the following to be safe:
Code:MyClass mc();
If I create my own class, MyClass and then declare it my code as follows:
Is it guaranteed to call its default constructor?Code:MyClass mc;
Or should I declare the following to be safe:
Code:MyClass mc();
Yes, it will call the constructor with no arguments or the one will all default arguments.
>>MyClass mc();
This declares a function "mc" that returns MyClass and takes void.
gg
C++ supports both styles. This because this version:
MyClass mc();
can also be a function prototype. Sometimes the compiler cant distinguish between a default construction and a function prototype and gives an error.
Consider this:
class A
{
};
A a();
int main()
{
}
Is a a global object or a function declaration returning A and taking no params? For this case you can do this.
A a;