class A
{
public:
int* get() { return &v; }
protected:
int v;
};
void foo(int* p);
int main()
{
foo(A().get()); // is this safe?
}
The temporary object created by A() needs to live long enough for the foo() function to execute and complete, otherwise the pointer goes bad. Is this a safe assumption? Or does the A() object die before then?
If I had to guess, I would say the above is perfectly safe to do, however I figured I should ask here anyway.
class A
{
public:
int* get() { return &v; }
protected:
int v;
};
void foo(int* p);
int main()
{
A myClass;
foo( myClass.get() ) //I don't think you can call functions in the temporary variable. I may be wrong
}