I am facing a GCC warning that I want to fix. Basically I am passing to a method a pointer to a local variable, which in my case is perfectly OK. I understand why the compiler tells me that this is a potential problem, but in my case this is OK.
How can I workaround it, on a local space? Passing -fpermissive
when compiling will make me fail to find future problems. I want to fix this specific problem, or workaround it.
Code is available here:
#include <cstdio>
class Integer{
public:
Integer(int i ){ v = i; };
int value(){ return v; };
private:
int v;
};
int foo(Integer *i);
int main()
{
foo( &Integer(12) );
}
int foo(Integer *i)
{
std::printf("Integer = %d\n", i->value());
}
And compilation gives me:
$ g++ test-reference.cpp -O test-reference
test-reference.cpp: In function ‘int main()’:
test-reference.cpp:15:18: error: taking address of temporary [-fpermissive]
$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu3) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EDIT:
Using const
(as in making foo
take a const pointer, and marking value()
as const) gives the same error.