Jump to content
• ### What is your GameDev Story?

• Advertisement

# overloading the comma operator

This topic is 4905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I discovered today that the comma (,) operator can be overloaded in C++. This will solve the last remaining niggle in my vector library, which is assigning arbitry values to the vector using the = operator.
Vector3 myvec;

myvec = 1.2, globalvec.y, (float) rand ();


The above would get translated into
myvec.x = 1.2;
myvec.y = globalvec.y;
myvec.z = (float) rand ();


One problem though - how can I get the comma operator to stack with the previous one? I'm assuming some kind of proxy/flyweight object is involved, but I would like to keep this as lightweight as possible. Any ideas? High amounts of ++ are up for grabs ;).

#### Share this post

##### Share on other sites
Advertisement
You can't overload the comma operator for built-in primitives like float. In order to get it to work, you'll need to 'kick start' the comma chain with a user defined type.

So you're code will look like:

myvec = Something , 1.2 , globalvec.y, (float)rand();

Of course if you do that you won't be saving much typing over just doing

myvec = Vector3(1.2, globalvec.y, (float)rand());

So with that said, do you still want to know how to get the comma operator to work with your assignment?

#### Share this post

##### Share on other sites
Off hand, I'd say

struct Vector3{   float x, y, z;   AssignProxyY operator=(float x_)   {      x = x_;      return AssignProxyY(this);   }   struct AssignProxyZ   {      Vector3* vec3;      explicit AssignProxyZ(Vector3* vec3) : vec3(vec3) {}      void operator,(float z_)      {         vec3->z = z;      }   };      struct AssignProxyY   {      Vector3* vec3;      explicit AssignProxyY(Vector3* vec3) : vec3(vec3) {}      AssignProxyZ operator,(float y_)      {         vec3->y = y;         return AssignProxyZ(vec3);      }   };};

Use at your own risk.

EDIT - except that it won't work due to the low priority of operator=. Listen to SiCrane. a = b, c, d is a = (b, c, d), not (a = b), c, d

#### Share this post

##### Share on other sites
EDIT: above posted just said what I was thinking

#### Share this post

##### Share on other sites
Overloading the comma operator is a bad idea in general because the results are nonintuitive. And you typically have to surround the expression to be overloaded with parentheses to supercede the low precedence of the comma operator, e.g.,

DictionaryEntry* de = /* array */;
de[0] = ("allotrope", Lexicals.Noun, "a structurally distinct form of an element or molecule");
de[1] = ("amorphous", Lexicals.Adjective, "without form or shape");
de[2] = ("tergiversation", Lexicals.Noun, "the act of evasion or escape");
// ... etc.

To use this, your class definition would contain something like:

DictionaryEntry operator,(const std::string& word, const Lexicals& wordType, const std::string& wordDefinition)
{
DictionaryEntry t(word, wordType, wordDefinition);
return t;
}

#### Share this post

##### Share on other sites
Surely assignment is higher in precedence than comma; for example the Blitz++ scientific computing library utilizes the comma operator for initializing things.

C operator precedence table

#### Share this post

##### Share on other sites
yep, the comma is as low as they go.

#### Share this post

##### Share on other sites
Mental: But that is a good thing: it means that Fruny's example works (with Blitz++, they do it the same way, by returning a proxy from the assignment operator).

#### Share this post

##### Share on other sites
Quote:
 Original post by kSquaredOverloading the comma operator is a bad idea in general because the results are nonintuitive. And you typically have to surround the expression to be overloaded with parentheses to supercede the low precedence of the comma operator, e.g.,DictionaryEntry* de = /* array */;de[0] = ("allotrope", Lexicals.Noun, "a structurally distinct form of an element or molecule");de[1] = ("amorphous", Lexicals.Adjective, "without form or shape");de[2] = ("tergiversation", Lexicals.Noun, "the act of evasion or escape");// ... etc.To use this, your class definition would contain something like:DictionaryEntry operator,(const std::string& word, const Lexicals& wordType, const std::string& wordDefinition){   DictionaryEntry t(word, wordType, wordDefinition);   return t;}

The , operator cannot be overloaded to have three parameters unfortunately.

#### Share this post

##### Share on other sites
Quote:
Original post by Mxz
Quote:
 Original post by kSquaredOverloading the comma operator is a bad idea in general because the results are nonintuitive. And you typically have to surround the expression to be overloaded with parentheses to supercede the low precedence of the comma operator, e.g.,DictionaryEntry* de = /* array */;de[0] = ("allotrope", Lexicals.Noun, "a structurally distinct form of an element or molecule");de[1] = ("amorphous", Lexicals.Adjective, "without form or shape");de[2] = ("tergiversation", Lexicals.Noun, "the act of evasion or escape");// ... etc.To use this, your class definition would contain something like:DictionaryEntry operator,(const std::string& word, const Lexicals& wordType, const std::string& wordDefinition){ DictionaryEntry t(word, wordType, wordDefinition); return t;}

The , operator cannot be overloaded to have three parameters unfortunately.

D'oh, of course you're right. The example is still valid, however; just remove one of the other two parameters.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 13
• 9
• 9
• 15
• 14
• Advertisement
• ### Forum Statistics

• Total Topics
634070
• Total Posts
3015330
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!