From my understanding of declarations and definitions, at the global scope:
MyClass instance();//Declares a function that returns a MyClass
MyClass instance;//Declares an instance of MyClass
Is it possible to declare a variable and define it to use the default constructor at global scope? What if I was using a structure instead of a class?
EDIT:
Okay, so MyClass instance; does call the default constructor. Can anyone explain how this is consistent with this example:
int a; // not default constructed, will have random data
int b = int(); // will be initialised to zero
will invoke the default constructor (i.e. the constructor with no parameters).
This is counter-intuitive, because to invoke an overloaded constructor with parameters you would do the following:
Logic would tell you that you pass in an empty argument list to invoke the default constructor, but the following code…
…looks like a prototype to the compiler rather than the construction of a
MyClassobject.There is no difference between a
structand aclassin C++, except that astructhaspublicmembers by default and aclasshasprivatemembers by default.