强制返回的对象被赋值 [英] force returned object to be assigned
问题描述
是否有一种方法,在C ++中,强制分配函数的返回值?
ie如果我有一个成员函数foo
class myClass {
...
public:
T1 foo(T2 x){T1 y; / * something * / return y;};
}
我可以在main()中调用
myClass obj;
T1 a = obj.foo(x); //< -
我可以进行更简单的调用
myClass obj;
obj.foo(x); // < -
(不存储返回的值) / p>
或者,我可以区分
的定义T1 a = obj .foo(x);
obj.foo(x);
感谢您的时间,对不起我的无知。
解决方案不,没有办法强制对返回的对象进行操作。
区分
T1 a = obj.foo(x);
obj.foo(x);
(至少不是以可移植的标准方式。堆栈等,但你为什么)。
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
解决方案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).
这篇关于强制返回的对象被赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!