Cascading just means you can call a function (often the same one) on the result. How doesn't actually matter. Generally you do this by returning a reference to *this, so that the next function call is to the same object as the previous. If the function returns by value, the next function call is executed on the temporary return object instead.
Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <algorithm>
class Matrix {
public:
int a, b, c, d;
Matrix(int A, int B, int C, int D) : a(A), b(B), c(C), d(D) { }
Matrix transpose() {
std::swap(b, c);
return *this;
}
void print() { printf("%d\t%d\n%d\t%d\n",a, b, c, d); }
};
int main() {
Matrix one(1, 5, 9, 13);
Matrix two = one.transpose();
Matrix three = one.transpose().transpose();
one.print();
two.print();
three.print();
return 0;
}
|
1 5
9 13
1 9
5 13
1 9
5 13 |
As you can see, matrix one is only transposed twice: once in the second line of the main and once in the third line of the main. However, if I change the return type of transpose to Matrix&, this happens:
1 9
5 13
1 9
5 13
1 9
5 13
|
Matrix one is now transposed three times: once in the second line of the main and
twice in the third line of the main. That is why references are so interesting: rather than returning a copy, it returns the object itself.
If you want "one" to stay the same, you have two options:
1) Have transpose make a copy of *this, transpose the copy and return it. This can be chained, because the intermediate copies are useless anyway. The final result is saved into Two by means of a copy.
2) Copy one to two and three and then call a transpose that changes and returns *this (by reference!).
Either method requires some copying. Because of the way it's set up, it's always going to be explicit copying. If you want implicit copying, move the transpose function outside of your class definition:
Matrix transpose(Matrix rhs) { ... return rhs; }
This way, the copying is done automatically (parameter passing by value = make a copy to work on). While this might no longer be called chaining, it's essentially the same, even if it looks a bit different:
Matrix three = transpose(transpose(one));