3

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
Community
  • 1
  • 1
Casebash
  • 114,675
  • 90
  • 247
  • 350

3 Answers3

8
MyClass instance;

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:

MyClass instance(param1, param2);

Logic would tell you that you pass in an empty argument list to invoke the default constructor, but the following code...

MyClass instance();

...looks like a prototype to the compiler rather than the construction of a MyClass object.

There is no difference between a struct and a class in C++, except that a struct has public members by default and a class has private members by default.

LeopardSkinPillBoxHat
  • 28,915
  • 15
  • 75
  • 111
  • Why does this behave differently from how `int` works (see edit above)? – Casebash Dec 13 '10 at 22:32
  • @Casebash - `int` is a POD type, which will either be zero-initialized or uninitialized depending on how it is declared. When you declare an `int` locally on the stack and don't explicitly initialize it, it will have random data as you have indicated. Please see this answer for more details: http://stackoverflow.com/questions/3102096/when-do-c-pod-types-get-zero-initialized/3102134#3102134 – LeopardSkinPillBoxHat Dec 13 '10 at 22:42
  • Suppose `MyStruct` is defined as `struct MyStruct { int a;};`. The answer you linked to seems to imply that `MyStruct` is not default initialised if it is declared in local scope (it says that myInstance.a is undefined). The default constructor for a struct calls the default constructors for each element in the structure, right? – Casebash Dec 13 '10 at 22:57
  • @Casebash - Yes, the default constructor for a `struct` or `class` will default-initialize all members. – LeopardSkinPillBoxHat Dec 14 '10 at 02:10
  • @LeopardSkinPillBoxHat [how to do `MyClass instance;` if the constructor of `MyClass` requires arguments](https://stackoverflow.com/q/67550551)? – user2284570 May 15 '21 at 20:21
  • @user2284570 - I wrote this answer 11 years ago when I was writing C++ regularly but I'm a bit out of practice with this, and C++ has changed heaps since I was using it. So I'd probably trust the people answering on your other question more than me :-) – LeopardSkinPillBoxHat May 16 '21 at 11:10
2
  1. It doesn't matter whether you're at global scope or not.
  2. MyClass instance; is a definition (using the default constructor, not just a declaration. To get only a declaration (e.g. in a header file), you would use extern MyClass instance;.
  3. It doesn't matter, for this part, whether MyClass is a class or a struct. The only thing that changes between structs and classes in C++ is the default interpretation of whether members and bases are public or private.
  4. If you want to be explicit, you could write MyClass instance = MyClass();.
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
1
MyClass instance;

is also a definition, using the default constructor. If you want to merely declare it you need

extern MyClass instance;

which is not a definition. Both, however, have external linkage.

Yttrill
  • 4,725
  • 1
  • 20
  • 29
  • [how to do `MyClass instance;` if the constructor of `MyClass` requires arguments](https://stackoverflow.com/q/67550551)? – user2284570 May 15 '21 at 20:21