The World of Mayukh Bose

<< Back to Main Page Mayukh's world: e-mail me | about
Mayukh's World: Operator Overloading With C++ Thursday, November 23, 2017
Index
  • Introduction
  • What Operators?
  • Rules
  • Assignment Operator
  • More on Assignment
  • Arithmetic Operators
  • Arithmetic with Globals
  • Increment/Decrement
  • Operator-Assignment
  • Unary Operators
  • Relational Operators
  • Bitshift/Extraction
  • Subscript Operator
  • Function Call Operator
  • Bit and Logical Ops
  • Comma Operator
  • Pointer to Member
  • new and delete Ops
  • Credits and Thanks
  • My Free Software
  • Delphi/C++ Builder
  • Pocket PC
  • FreeSpeech Chat
  • C/C++ Freebies
  • Perl
  • Python
  • Ruby
  • C++ Operator Overloading Tutorial e-mail me

    The Comma Operator ,

    The comma operator is a binary operator and we can overload it similar to any other binary operator (as we saw in the previous section). However it is usually NOT overloaded by most people. The reason is that it is already used in C++ for other things and overloading it only serves to confuse users. Another reason is that the comma operator is very low in the operator precedence order, hence you may need to use parenthesis to force the C++ compiler to use your overloaded function. In fact, Bjarne Stroustrup even mentions in his "The Design and Evolution of C++" book that he only allowed the comma operator to be overloaded for orthogonality and didn't actually see any practical uses for it.

    One use for the comma operator is actually used by a few commercial C++ libaries. It is useful in initializing data structure objects.
          Array a(5);
          a = 10, 20, 30, 40, 50; // Initialize the 5 elements
        
    This is possible because the C++ compiler treats the expression like this:
          (((((a = 10), 20), 30), 40), 50)
        
    For this to work, you need to overload the = operator first to accept a single value and return an Array object. Then, you can overload the comma operator between an Array and an int, to keep pushing extra values into the array object as needed. The Blitz++ library is one user of this technique.

    Another use for it would be to overload a name=value type object. Consider that we have the following:
          class KeyVal {
          private:
            std::string name;
    	int value;
          public:
            KeyVal() { name = ""; value = 0; }
    	KeyVal(const string& key, const int val) { name = key; value = val; }
    	void PrintIt() { std::cout << name << " = " << value << endl; }
        };
        
    Now, we are going to overload the comma(,) operator to instantiate an object as follows:
          KeyVal operator,(const std::string& key, const int& val) {
            KeyVal tmp(key, val);
    	return tmp;
          }
        
    Thus, instead of doing something like this:
          KeyVal kv;
          string key = "foo"; int value = 123;
          kv = KeyVal(key, value);
          kv.PrintIt();
        
    a coder could potentially do this:
          KeyVal kv;
          string key = "foo"; int value = 123;
          kv = (key, value); // No need to typecast to KeyVal
          kv.PrintIt();
        
    Note that the parenthesis around the (key, value) expression. They are necessary because the C++ compiler won't accept it as a call to the overloaded operator function without them.

    This function is likely to confuse lots of C++ programmers though, since we are used to seeing a constructor in the expression. Thus the value of this code may be slightly dubious. However, we've seen exactly how the comma operator is overloaded.


    <<Previous: Bit and Logical Operators ^Up to Mayukh's World^ Next: Pointer to Member Operator >>

    Copyright © 2004 Mayukh Bose. All rights reserved.
    This work may be freely reproduced provided this copyright notice is preserved.
    OPTIONAL: Please consider linking to this website (http://www.mayukhbose.com/) as well.