A
Andy Gibbs
Hello,
I have a problem with the following example code:
#include <stdio.h>
class Test
{
public:
Test(char* str)
{
printf("ctor\n");
_str = str;
}
~Test()
{
printf("dtor\n");
}
char* _str;
};
void Function(const Test* const t)
{
if (t) printf("str=%s\n", t->_str);
else printf("null\n");
}
int main(int argc, char *argv[])
{
printf("test1:\n");
{ Test t("test1"); Function(&t); }
printf("test2:\n");
Function(&Test("test2"));
printf("test3:\n");
Function(0);
}
The problem comes in the line "Function(&Test("test2"));" with the
warning "taking address of temporary". I am using GCC 4.1.2. However, the
code runs as expected with the following output:
test1:
ctor
str=test1
dtor
test2:
ctor
str=test2
dtor
test3
null
The code above demonstrates that the Test object is not destroyed until
after Function is called, so I cannot see why the compiler complains about
this. However, for my actual application, this particular syntax is what I
require in order to keep the code at its most legible, and so I really hope
to find a solution to this warning.
Is there some way I should alter the signature of Function to stop the
warning? Is there a compiler option to ignore this specific warning (e.g.
a #pragma or something)?
Thanks for any help!
Andy
I have a problem with the following example code:
#include <stdio.h>
class Test
{
public:
Test(char* str)
{
printf("ctor\n");
_str = str;
}
~Test()
{
printf("dtor\n");
}
char* _str;
};
void Function(const Test* const t)
{
if (t) printf("str=%s\n", t->_str);
else printf("null\n");
}
int main(int argc, char *argv[])
{
printf("test1:\n");
{ Test t("test1"); Function(&t); }
printf("test2:\n");
Function(&Test("test2"));
printf("test3:\n");
Function(0);
}
The problem comes in the line "Function(&Test("test2"));" with the
warning "taking address of temporary". I am using GCC 4.1.2. However, the
code runs as expected with the following output:
test1:
ctor
str=test1
dtor
test2:
ctor
str=test2
dtor
test3
null
The code above demonstrates that the Test object is not destroyed until
after Function is called, so I cannot see why the compiler complains about
this. However, for my actual application, this particular syntax is what I
require in order to keep the code at its most legible, and so I really hope
to find a solution to this warning.
Is there some way I should alter the signature of Function to stop the
warning? Is there a compiler option to ignore this specific warning (e.g.
a #pragma or something)?
Thanks for any help!
Andy