anything whcih can be done with ternary but not with if else

S

shailendra

Ali said:
if-else requires assignability. Anything that doesn't support assignment
cannot be used with if-else. In addition to the already provided constructor
initialization list, three more that I can think of:


1) A class that doesn't define operator=:

Class UnAssignable{ /* ... */ };

/* copy construction */
UnAssignable object = condition ? first : second;


2) Initializing a reference:

Class & reference = condition ? first : second;


3) Initializing a constant:

SomeType const object = condition ? first : second;

Ali

I don't think overloading of ternary operator is possible in C++
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

shailendra said:
I don't think overloading of ternary operator is possible in C++

Did you reply to my message by mistake? The reason I am asking is that I
can't understand what part you may be referring to. (The lack of o period at
the end of your sentence makes me believe that you really sent it by
mistake, at least prematurely.)

Maybe you thought that the pairs of first/second above were the same objects
in all three cases? If so, no, I didn't mean them to be so; think of each
pair as being defined separately in each case.

Even if I did mean to use the same first and second in all three cases, it
would still work, provided a suitable relationship (inheritance, constructor
parameter, existence of type conversion operators, etc.) among UnAssignable,
Class, SomeType, and the type used for first and second.

Ali
 
J

John Carson

Thomas J. Gritzan said:
int *p;
if (b)
p = &x;
else
p = &y;

int &ref2 = *p;

Thomas

Yes, a nice workaround (albeit one that comes at the cost of an extra
variable and extra assignment).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top