Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my overloaded comma operator get called?

I'm trying to overload the comma operator with a non-friend non-member function like this:

#include <iostream>
using std::cout;
using std::endl;

class comma_op
{
    int val;

public:
    void operator,(const float &rhs)
    {
        cout << this->val << ", " << rhs << endl;
    }
};

void operator,(const float &lhs, const comma_op &rhs)
{
    cout << "Reached!\n";      // this gets printed though
    rhs, lhs;                  // reversing this leads to a infinite recursion ;)
}

int main()
{
    comma_op obj;
    12.5f, obj;

    return 0;
}

Basically, I'm trying to get the comma operator usable from both sides, with a float. Having a member function only allows me to write obj, float_val, while having an additional helper non-friend non-member function allows me to write float_val, obj; but the member operator function doesn't get called.

GCC cries:

comma.cpp: In function ‘void operator,(const float&, const comma_op&)’:
comma.cpp:19: warning: left-hand operand of comma has no effect
comma.cpp:19: warning: right-hand operand of comma has no effect


Note: I realise that overloading operators, that too to overload comma op., Is confusing and is not at all advisable from a purist's viewpoint. I'm just learning C++ nuances here.

like image 218
legends2k Avatar asked Feb 25 '10 15:02

legends2k


People also ask

Can comma operator be overloaded?

In C++, we can overload the comma operator using Operator Overloading. For Example: For “Send the query X to the server Y and put the result in variable Z”, the “and” plays the role of the comma. The comma operator (, ) is used to isolate two or more expressions that are included where only one expression is expected.

How do you call an overloaded function in C++?

Overloading of function-call operator in C++ The function call operator is denoted by “()” which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.

Why we Cannot overload operators?

These operators cannot be overloaded because if we overload them it will make serious programming issues. For an example the sizeof operator returns the size of the object or datatype as an operand. This is evaluated by the compiler. It cannot be evaluated during runtime.

Which operator you Cannot overload?

You cannot overload the following operators: . You cannot overload the preprocessor symbols # and ## . An operator function can be either a nonstatic member function, or a nonmember function with at least one parameter that has class, reference to class, enumeration, or reference to enumeration type.


1 Answers

void operator,(const float &rhs)

You need a const here.

void operator,(const float &rhs) const {
    cout << this->val << ", " << rhs << endl;
}

The reason is because

rhs, lhs

will call

rhs.operator,(lhs)

Since rhs is a const comma_op&, the method must be a const method. But you only provide a non-const operator,, so the default definition will be used.

like image 191
kennytm Avatar answered Oct 14 '22 07:10

kennytm