Alex Rivera | Logout

C++ spooky constructor

Asked 2011-12-12T14:29:55.833
11

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

Lets have this code

class Foo {
  Foo(int) { }
};

Then we have there results:

int main() {
  Foo f1 = Foo(5); // 1: OK, explicit call
  Foo f2(5); // 2: OK, implicit call
  Foo f3(); // 3: no error, "f3 is a non-class type Foo()", how so?
  Foo f4(f1); // 4: OK, implicit call to default copy constructor
  Foo f5; // 5: expected error: empty constructor missing
}

Can you explain what's happening in case 3?

Edit
Report

2 Answers

8

Foo f3(); declares a function called f3, with a return type of Foo.

answered 2011-12-12T14:32:12.500
4
  • f1 invokes the copy constructor after an explicit call, you were wrong on this one
  • f2 is an explicit constructor call // you were wrong here too
  • f3 declares a function
  • f4 is again the copy constructor, like f1 // you're right here
  • f5 would calls the default constructor // you're right here again
answered 2011-12-12T14:33:43.257

Your Answer