I ran across some code that did something like this. I can see where obj.method().method().method(); might be quicker to type and even read depending on what obj might look like typed out. But wouldn't it be faster to not have method() return a reference to the object and just type out obj. each time you call the method, or does the compiler optimize that out? I had never seen it done this way before and it threw me for a loop before I realized what method() returned, especially with it written down instead of across like in my example below.
Example:
class test
{
public:
test():x(0){}
test& add(int i) {x+=i; return *this;}
int x;
};
int main()
{
test t;
t.add(1)
.add(2)
.add(3);
return 1;
}
PS. What is the syntax to put the code in code blocks?
Example:
class test
{
public:
test():x(0){}
test& add(int i) {x+=i; return *this;}
int x;
};
int main()
{
test t;
t.add(1)
.add(2)
.add(3);
return 1;
}
PS. What is the syntax to put the code in code blocks?