By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

comma

P: n/a
I am looking for a good example of overloading operator , (operator comma)

Any suggestions?

Ben
Aug 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
benben wrote:
I am looking for a good example of overloading operator
, (operator comma)

Any suggestions?


Don't :)

But if you insist, blitz++ has matrix initalisation with
comma overloading, a la:
Array<double,2> a =
1, 2, 3,
4, 5, 6,
7, 8, 9;

There's also a boost library with such things for filling
std containers.

Marc
Aug 23 '05 #2

P: n/a
benben wrote:
I am looking for a good example of overloading operator , (operator comma)

Any suggestions?

Ben


You should not overload the comma operator unless you really have to
(Very good explained in Sutter, Alexandrescu: "C++ Coding Standards", I
will give a short summary):

The build-in comma operator gurantees that the expression before the
comma is evaluated before the expression after it:

typedef SOMETHING T;
....
T a = 42;
T b = ++a, ++a;

With T=int and the build-in comma operator this will assign 44 to b.
(The first (++a) expression is evaluated first, its result discarded,
and then the result of the second (++a) is returned)

If T is a user defined type and you have overloaded the comma operator
for this type, the order in which the arguments are evaluated, is
undefined, so the above example would be undefined - it could be 43 as
well es 44.

You should step back from overloading the comma operator, because you
cannot simulate the build-in behaviour.

Gabriel
Aug 25 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.