About constructors with no parameters

I have got a compile error and searched around for a solution and found it. But can someone tell me why this solution should work? Thanks!

Class myClass
{
myClass();
};

Problem:
myClass instance1(); // Doesn't work although the constructor is defined and has zero parameters.

Solution:
myClass instance1; // Works by removing the pair of parentheses.

Question: why do I have to remove the pair of parentheses when my constructor has zero parameters? ::slight_smile:

It is possibly an ambiguity. myClass instance1(); could either be a constructor call or a forward declaration of a function called instance1 taking no arguments and returning a myClass.

What error do you get?

The following is the prototype declaration of a function whose return data type is a myClass object. The function has no arguments.

myClass instance1();

If you ever decide to call the function, you will have to supply an implementation (function body) some place where the compiler can find it. If you never invoke the function, the above statement is still perfectly valid C++ (assuming the class has been defined when the compiler encounters it), but it does not create executable code.

Note that in C++ a function prototype with no arguments is the same as if you had declared it

myClass instance1(void);

Many C++ programmers prefer the explicit "void" parameter list in hopes of decreasing the probability of us mere humans becoming confused. The compiler never gets confused with stuff like this, but for the rest of us, well...

On the other hand, the following calls the myClass constructor to create an object of that class. The name of the object is declared to be "instance2"

myClass instance2;

Regards,

Dave