Why GCC 4.7 complains when instantiate a class inside a function (with a pointer)?
Bad:
#include "foo.h"
int fn () {
Foo *foo;
foo->method();
return 0;
}
main.cpp: In member function 'int foo()': main.cpp:21:52: warning: 'fn' may be used uninitialized in this function [-Wuninitialized]
Good:
#include "foo.h"
Foo *foo;
int fn () {
foo->method();
return 0;
}
Good:
#include "foo.h"
int fn () {
Foo foo;
foo.method();
return 0;
}
There's a difference between Foo * foo;
and Foo foo;
The first declares a pointer to a Foo, the second declares & invokes the default-constructor of a Foo.
EDIT: Maybe you meant to write Foo * foo= new Foo();
, in order to allocate a Foo
on the heap which can outlive the function call.