0

Say I have an extremely simple Foo class like so:

#include <iostream>

class Foo
{
    public:
        Foo() {std::cout << "bar" << std::endl;}
};

I have always thought that both Foo foo and Foo foo() would output bar, as they both call the constructor. But when I try:

int main()
{
    Foo foo();
    return 0;
}

Nothing gets outputted. Is there a reason for this? How can I make sure that the constructor gets called in both instances?

Ideone test

Ben Hollier
  • 585
  • 2
  • 11
  • 32
  • 4
    This line `Foo foo()` is a function declaration, it does not construct a `Foo` object. – PaulMcKenzie Jul 31 '16 at 11:04
  • @splrs I fixed it, the code in the question I wrote from the editor without testing it, instead of copying it from the code I was compiling – Ben Hollier Jul 31 '16 at 11:18

0 Answers0