What does the following code do?
Is a Foo object created and then immediately destroyed? I've seen this used in a few examples and I don't understand what the purpose of Foo() is here.Code:class Foo
{};
int main()
{
Foo();
}
Printable View
What does the following code do?
Is a Foo object created and then immediately destroyed? I've seen this used in a few examples and I don't understand what the purpose of Foo() is here.Code:class Foo
{};
int main()
{
Foo();
}
When you declare a function Foo(); inside the class Foo, it will be a constructor...
in that case it does nothing... but for instance, if you had a class like this
when you declared an object with a and b it would call the constructor...Code:Class Foo{
public:
int x, int y;
Foo(int a, int b){ x = a; y = b; }
};
and then FooObj.x would be 1 and FooObj.y would be 2... something like this :P...Code:Foo FooObj(1, 2);
I was more referring to the fact that main calls a constructor without binding the created object to a variable.
Can you give us a real example, with Foo defined?
What's wrong with the example above? It compiles without warnings, and I have no idea what it does.
In particular, if Foo is a class, what does this do when used outside the class body:
Code:Foo();