467,076 Members | 993 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,076 developers. It's quick & easy.

comma

I am looking for a good example of overloading operator , (operator comma)

Any suggestions?

Ben
Aug 23 '05 #1
  • viewed: 1865
Share:
2 Replies
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
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.

Similar topics

27 posts views Thread by Alberto Vera | last post: by
5 posts views Thread by Derek | last post: by
11 posts views Thread by Shawn Odekirk | last post: by
4 posts views Thread by G Patel | last post: by
21 posts views Thread by siliconwafer | last post: by
9 posts views Thread by Wayne | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.