tags:

views:

421

answers:

4

Hi, I have a question.

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.

+4  A: 

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

Nemanja Trifunovic
Related? I'd call it a duplicate, myself.
Paul Tomblin
+16  A: 

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
Not a function pointer (that would be `Example (*ex)()`) but a function prototype.
Konrad Rudolph
@Konrad, thanks for the correction
JaredPar
+8  A: 

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

Konrad Rudolph
+2  A: 

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.

caspin