1

Possible Duplicate:
Why is it an error to use an empty set of brackets to call a constructor with no arguments?

I bumped into the following problem. I created 2 instances of foo. Then I realized, that foo f(); didn't execute the contructor of a class. Why is that?

class foo{
public:
    foo() {cout <<"executed contructor...";}
};

int main() {
    foo f(); // doesn't run the ctor???? why?
    foo f2; // this one does execute the ctor


    system("pause");
    return 0;
}
Community
  • 1
  • 1
Novellizator
  • 13,633
  • 9
  • 43
  • 65
  • 2
    See this previous question http://stackoverflow.com/questions/180172/why-is-it-an-error-to-use-an-empty-set-of-brackets-to-call-a-constructor-with-no – msgmash.com Feb 05 '12 at 15:38
  • 1
    `foo f3(foo());` is an example of the most vexing parse. `foo f();` is just a slightly vexing parse. – CB Bailey Feb 05 '12 at 15:43

2 Answers2

6

The first declares a function. Try to access the object named f. The compiler will complain along the lines: f has non class type foo (), which means it is a function taking no arguments and returning an object of type foo.

pmr
  • 58,701
  • 10
  • 113
  • 156
2

Check C++ FAQ question 10.2:

[10.2] Is there any difference between List x; and List x();?

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2

ouah
  • 142,963
  • 15
  • 272
  • 331