Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualServant of the Lord

Posted 27 July 2012 - 11:12 AM

Overloading the comma operator for that usage is considered evil. Anytime you overload an operator for a usage that departs from what is normal, you create potential confusion for users using your code. Adding to the evilness, commas are used multiple places in C++ for multiple different purposes (unlike other operators)... but only one usage of the comma overloadable, and has lower priority than the others, which may lead to more confusion from time to time.

A much better initialization would be the more common, and thus more socially (among programmers) acceptable:
m = {1, 0, 0, 1};

However, if you want to go ahead and do it anyway, here's how:
MyClass &MyClass::operator ,(int value)
{
	 //...do whatever with 'value'.

	 return *this; //Return a reference to myself.
}

You overload the operator, handle the (one!) parameter, then you return a reference to yourself for the next operator to work on.
myClass , a, b, c, d

Is parsed as:
((((myClass , a) , b) , c) , d)

...as the return value is a reference to yourself to call the next operator.

If you want to use the more preferable way:
//Initialize:
MyClass m = {1, 0, 0, 1};

//Or assign later:
m = {0, 2, 11, 17};

...you can do so through the new C++11's standard std::initializer_list<>, if you are using an up-to-date compiler with C++11 enabled. This is what the standard library does now.

#2Servant of the Lord

Posted 27 July 2012 - 11:11 AM

Overloading the comma operator for that usage is considered evil. Anytime you overload an operator for a usage that departs from what is normal, you create potential confusion for users using your code. Adding to the evilness, commas are used multiple places in C++ for multiple different purposes (unlike other operators)... but only one usage of the comma is the one being overloaded, which may confuse you from time to time.

A much better initialization would be the more common, and thus more socially (among programmers) acceptable:
m = {1, 0, 0, 1};

However, if you want to go ahead and do it anyway, here's how:
MyClass &MyClass::operator ,(int value)
{
	 //...do whatever with 'value'.

	 return *this; //Return a reference to myself.
}

You overload the operator, handle the (one!) parameter, then you return a reference to yourself for the next operator to work on.
myClass , a, b, c, d

Is parsed as:
((((myClass , a) , b) , c) , d)

...as the return value is a reference to yourself to call the next operator.

If you want to use the more preferable way:
//Initialize:
MyClass m = {1, 0, 0, 1};

//Or assign later:
m = {0, 2, 11, 17};

...you can do so through the new C++11's standard std::initializer_list<>, if you are using an up-to-date compiler with C++11 enabled. This is what the standard library does now.

#1Servant of the Lord

Posted 27 July 2012 - 10:06 AM

Overloading the comma operator for that usage is considered evil. Anytime you overload an operator for a usage that departs from what is normal, you create potential confusion for users using your code. Even worse, commas are used multiple places in C++ for multiple different purposes (unlike other operators)... but only one usage of the comma is the one being overloaded, which may confuse you from time to time.

A much better initialization would be the more common, and thus more socially (among programmers) acceptable:
m = {1, 0, 0, 1};

However, if you want to go ahead and do it anyway, here's how:
MyClass &MyClass::operator ,(int value)
{
     //...do whatever with 'value'.

     return *this; //Return a reference to myself.
}

You overload the operator, handle the (one!) parameter, then you return a reference to yourself for the next operator to work on.
myClass , a, b, c, d

Becomes:
((((myClass , a) , b) , c) , d)

...as the return value is a reference to yourself to call the next operator.

If you want to more preferable:
//Initialize:
MyClass m = {1, 0, 0, 1};

//Or assign later:
m = {0, 2, 11, 17};

...you can do so through the new C++11's standard std::initializer_list<>, if you are using an up-to-date compiler with C++11 enabled. This is what the standard library does now.

PARTNERS