Acorbe Acorbe - 2 months ago 10
C++ Question

force returned object to be assigned

is there a way, in C++, to force the assignation of the returned value of a function?
i.e. if I have a member function foo

class myClass{
...
public:
T1 foo(T2 x){T1 y; /*something*/ return y;};
}


which I can call in the main() as

myClass obj;
T1 a = obj.foo(x); //<--


can I make the simpler call

myClass obj;
obj.foo(x); //<--


(that does not store the returned value) somehow "illegal"?

Alternatively, can I distinguish the definitions of

T1 a = obj.foo(x);
obj.foo(x);


thank you for your time and sorry for my ignorance

Answer

No, there's no way to force an operation on a returned object.

And no, you can't distinguish between

T1 a = obj.foo(x);
obj.foo(x);

inside the calls (at least not in a portable, standard way. You could hack away with call-stacks and such, but why would you).

Comments