小编WeR*_*lic的帖子

返回 void 或对 self 的引用?

鉴于以下类:

struct Object {
    int x, y;
    void addtoall( int value ){ x += value; y += value; };
    Object& addtoall( int value ){ x += value; y += value; return *this; };
};
Run Code Online (Sandbox Code Playgroud)

这两个成员函数有什么区别?

我知道对于某些运算符重载(例如: operator+= )需要返回对 self 的引用,但不包括运算符重载,是否有必要?如果没有,您什么时候想要或需要返回对 self 的引用而不是返回 void?

如果这可以通过 google-fu 找到,或者是一个非常基本的问题,我深表歉意,但我不确定究竟要搜索什么(而不是因为缺乏尝试)。

c++ reference void method-chaining

3
推荐指数
1
解决办法
738
查看次数

标签 统计

c++ ×1

method-chaining ×1

reference ×1

void ×1