2

Following is a fancy code:

class B
{
    private:
        int sum;

    public:
        B ()
        {
            sum = 0;
        }

        B& add (int number)
        {
            sum =+ number;
            return *this;
        }
};

int main ()
{
    B obj;
    obj.add (1).add (2).add (3). add (4);
}

Fine, what are the "serious" uses of returning the this pointer from a function call?

hochl
  • 12,524
  • 10
  • 53
  • 87
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

5 Answers5

4

An example would be;

class basic_ostream
    : ...
{
    basic_ostream& operator<<(bool n);

You really want to return this to be able to chain to;

std::cout << boolValue << std::endl;
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
2
mycode $ fgrep -r 'return *this' /usr/include/c++/4.4.3 | wc -l
592
mycode $
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • 5
    @Anisha: the point is that there are 592 uses of `return *this` in GCC's library headers. Presumably all or most of these are "serious", so you can look at any of them for examples. – Steve Jessop Mar 13 '12 at 16:38
2

One use of this is for the Named Parameter Idiom. It depends on method chaining.

class Person;

class PersonOptions
{
  friend class Person;
  string name_;
  int age_;
  char gender_;

public:
   PersonOptions() 
   : age_(0), gender_('U')
   {}

   PersonOptions& name(const string& n) { name_ = n; return *this; }
   PersonOptions& age(int a) { age_ = a; return *this; }
   PersonOptions& gender(char g) { gender_ = g; return *this; }
};

class Person
{
  string name_;
  int age_;
  char gender_;

public:
   Person(const PersonOptions& opts) 
   : name_(opts.name_), age_(opts.age_), gender_(opts.gender_)
   {}
};
Person p = PersonOptions().name("George").age(57).gender('M');
Person p = PersonOptions().age(25).name("Anna");
Community
  • 1
  • 1
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
1

The initial reason wass for chaining mathematical operations, like this:

class mynumberclass {
    int internal;
public:
    mynumberclass(int);
    mynumberclass operator+(const mynumberclass&) const;
    mynumberclass operator-(const mynumberclass&) const;
    mynumberclass operator*(const mynumberclass&) const;
    mynumberclass operator/(const mynumberclass&) const;
    mynumberclass operator%(const mynumberclass&) const;
    mynumberclass& operator+=(const mynumberclass&);
    mynumberclass& operator-=(const mynumberclass&);
    mynumberclass& operator*=(const mynumberclass&);
    mynumberclass& operator/=(const mynumberclass&);
    mynumberclass& operator%=(const mynumberclass&);
};

int main() {
    mynumberclass a(3);
    mynumberclass b(4);
    mynumberclass c = (a * b + b) / 2; //this chains 3 of the above operators
}

without chaining, that code would have to look like this:

int main() {
    mynumberclass a(3);
    mynumberclass b(4);
    mynumberclass c(a);
    c *= b;
    c += b;
    c /= 2;
}

FredLarson also mentions the Named Parameter Idiom, which is certainly an awesome thing you can use chaining for.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
0

The first thing that comes to mind is chaining functions to perform operations on the same object. jQuery, although not C++, has shown that this can be a very useful paradigm.

Scott M.
  • 7,313
  • 30
  • 39