5

What does it mean to call a class like this:

class Example
{
 public: 
  Example(void);
  ~Example(void);
}

int main(void)
{
 Example ex(); // <<<<<< what is it called to call it like this?

 return(0);
}

Like it appears that it isn't calling the default constructor in that case. Can someone give a reason why that would be bad?

Thanks for all answers.

codelogic
  • 71,764
  • 9
  • 59
  • 54
Daniel
  • 374
  • 2
  • 5

4 Answers4

16

Currently you are trying to call the default constructor like so.

Example ex();

This is not actually calling the default constructor. Instead you are defining a function prototype with return type Example and taking no parameters. In order to call the default constructor, omit the ()'s

Example ex;
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
8

This declares a function prototype for a function named ex, returning an Example! You are not declaring and initializing a variable here.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
4

Does it even compile? Anyway, see this related topic.

Community
  • 1
  • 1
Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88
2

As has been noted Example ex(); declares a function prototype. Not what anyone would expect. This C++ wart will be fixed by the new C++0x standard. In the future the preferred syntax will be Example ex{};. The new uniform construction has many other nice features, see more here.

deft_code
  • 57,255
  • 29
  • 141
  • 224