One practical use is to use it effectively with variable arguments in a macro. By the way, variable arguments were previously extensions to GCC and are now part of the C ++ 11 standard.
Suppose we have a class X that adds an object of type A . i.e.
class X { public: X& operator+= (const A&); };
What if we want to add 1 or more objects A to X buffer; ?
For example,
#define ADD(buffer, ...) buffer += __VA_ARGS__
Above the macro, if used as:
ADD(buffer, objA1, objA2, objA3);
then it will expand to:
buffer += objA1, objeA2, objA3;
Therefore, this will be a great example of using the comma operator, since variable arguments expand with the same.
So, to resolve this, we overload the comma operator and wrap it around += , as shown below
X& X::operator, (const A& a) {
iammilind Aug 3 '14 at 22:13 2014-08-03 22:13
source share