![](/img/trans.png)
[英]why does we need to build a boost library ,isn't it sufficient to just include the head files ,as boost are template
[英]For cascading member function calls, why do i need to return the reference? Why isn't just the this pointer sufficient?
#include <iostream>
using namespace std;
class armon {
int a;
int b;
public:
armon(int newA, int newB) : a(newA), b(newB) {}
armon setA(int newA) {
a = newA;
return *this;
}
armon setB(int newB) {
b = newB;
return *this;
}
void print(void) { cout << a << endl << b; }
};
int main() {
armon s(3, 5);
s.setA(8).setB(9);
s.print();
}
返回this
指針也將足夠。 但是,級聯調用的語法需要在鏈的中間進行更改:
s.setA(8)->setB(9)->setC(10);
這看起來不一致,因此返回引用是更好的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.