H
Henning Hasemann
I have a function which gets the adress of an object as argument.
It does some comparsion with the object's contents and then returns.
no Reference or pointer to the object is stored or will be used after
the function has returned.
Say the function whould be named f and the objects class whould be T
it'll look like this:
bool f(T* thing) {
return thing->foobar == 5; // Just a stupid example
}
When I now write a piece of code like this:
bool yesorno = f(&T(77));
I get a warning because I take the address of a temporary object.
Im not sure if this is okay or not here.
Does the object live long enough so f can do its operations on it?
Is it generally a good idea to to such things?
Or should I create an extra local variable which holds T(77)?
(As I will have lots of these calls that whould be unhandy as I use
different subclasses of T and you can Imagine using new and delete for
every call whuld be even unhandier.)
TIA
Henning
It does some comparsion with the object's contents and then returns.
no Reference or pointer to the object is stored or will be used after
the function has returned.
Say the function whould be named f and the objects class whould be T
it'll look like this:
bool f(T* thing) {
return thing->foobar == 5; // Just a stupid example
}
When I now write a piece of code like this:
bool yesorno = f(&T(77));
I get a warning because I take the address of a temporary object.
Im not sure if this is okay or not here.
Does the object live long enough so f can do its operations on it?
Is it generally a good idea to to such things?
Or should I create an extra local variable which holds T(77)?
(As I will have lots of these calls that whould be unhandy as I use
different subclasses of T and you can Imagine using new and delete for
every call whuld be even unhandier.)
TIA
Henning