C++ Object Method Chaining


You might see some method chainning usage in JQuery like this:

1
$('#obj').animate(a).slideUp(b).fadeOut(c);
$('#obj').animate(a).slideUp(b).fadeOut(c);

Actually, this is very simple to implement, all we need to do is to return the object itself as a pointer rather than void type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Price {
    private:
        int v;
    public:
        Price(int _v) {
            v = _v;
        }
 
        Price* Raise(int offset) {
            v += offset;
            return this; // return this object for next chainning method
        }
 
        Price* Drop(int offset) {
            v -= offset;
            return this;// return this object for next chainning method
        }
 
        Price* Print() {
            cout << v << endl;
            return this;// return this object for next chainning method
        }
};
class Price {
    private:
        int v;
    public:
        Price(int _v) {
            v = _v;
        }

        Price* Raise(int offset) {
            v += offset;
            return this; // return this object for next chainning method
        }

        Price* Drop(int offset) {
            v -= offset;
            return this;// return this object for next chainning method
        }

        Price* Print() {
            cout << v << endl;
            return this;// return this object for next chainning method
        }
};

So after modification of internal attributes, we return this, which can be used to chain the next method.

1
2
Price x(10);
x.Raise(200)->Drop(50)->Print();
Price x(10);
x.Raise(200)->Drop(50)->Print();

The above example usage prints 160. Because we initialize the price to 10 and increment 200 and drop 50. This will make your code much cleaner and is a good OOP (Object Oriented) design principle.

Feature Comments

I prefer chaining using references in cpp. Allows to avoid strange and dangerous pointer operations. And, of course, it is more concise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Price {
    private:
        int v;
    public:
        Price(int _v) {
            v = _v;
        }
 
        Price& Raise(int offset) {
            v += offset;
            return *this; // return this object for next chainning method
        }
 
        Price& Drop(int offset) {
            v -= offset;
            return *this;// return this object for next chainning method
        }
 
        Price& Print() {
            cout << v << endl;
            return *this;// return this object for next chainning method
        }
};
class Price {
    private:
        int v;
    public:
        Price(int _v) {
            v = _v;
        }

        Price& Raise(int offset) {
            v += offset;
            return *this; // return this object for next chainning method
        }

        Price& Drop(int offset) {
            v -= offset;
            return *this;// return this object for next chainning method
        }

        Price& Print() {
            cout << v << endl;
            return *this;// return this object for next chainning method
        }
};

This is an example of the builder design pattern.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
339 words
Last Post: Greedy Algorithm to Compute the Largest Number
Next Post: Does CloudFlare (Cache Everything) Affect the Adsense?

The Permanent URL is: C++ Object Method Chaining

2 Comments

  1. Dora

Leave a Reply