Wanna make the class be able to do a chain-call
1
2
3
4
5
6
7
8
9
10
11
|
class TextBox {
string text;
TextBox addText(const string& append) {
text += append;
return this;
}
};
TextBox textBox1;
textBox1.addText("Text1").addText("Text2").addText("Text3");
|
This is not going to work :P
1
2
3
4
5
6
7
8
9
10
11
|
class TextBox {
string text;
TextBox& addText(const string& append) {
text += append;
return *this;
}
};
TextBox textBox1;
textBox1.addText("Text1").addText("Text2").addText("Text3");
|
Works fine!
Or you can
1
2
3
4
5
6
7
8
9
10
11
|
class TextBox {
string text;
TextBox* addText(const string& append) {
text += append;
return this;
}
};
TextBox textBox1;
(*(*textBox1.addText("Text1")).addText("Text2")).addText("Text3");
|
Longer, more verbose.