If I have a class Foo and a method with the prototype void bar(Foo* foo). Is it okay to do: bar(&Foo());?
I know it compiles and works, but since I’m relatively new to C++, I was wondering whether the instance created will be correctly destructed after usage, or if I have to worry about anything else when doing that.
That’s not even valid C++. Some compilers may be silly and accept it, but they shouldn’t do it. You cannot take the address of a temporary.
This restriction is given in section §5.3.1 of the C++ standard:
Temporaries are prvalues, so they cannot have their address taken.
Now, assuming your compiler accepts this, the pointer will be valid while the function call runs, but only while it runs. Once it returns the temporary is destroyed, so any pointers to it that still exist (like, if the function stored it) are invalid.