Using the ternary operator to initialize derived class objects

  • Thread starter Dwight Army of Champions
  • Start date
D

Dwight Army of Champions

Hello! Suppose I have two derived classes Car and Bicycle that derive
from a base class Vehicle and an integer x. Why does the following
code give an "operand types are incompatible" error...

Vehicle* AnyVehicle = (x % 2 == 0) ? new Car() : new Bicycle();

....but the following code, which utilizes a simple if statement to do
exactly the same thing, compiles successfully?

Vehicle* AnyVehicle;

if (x % 2 == 0) {
AnyVehicle = new Car();
}
else {
AnyVehicle = new Bicycle();
}

Is it even possible to initialize these objects with the ternary
operator?
 
G

Goran

Hello! Suppose I have two derived classes Car and Bicycle that derive
from a base class Vehicle and an integer x. Why does the following
code give an "operand types are incompatible" error...

Vehicle* AnyVehicle = (x % 2 == 0) ? new Car() : new Bicycle();

...but the following code, which utilizes a simple if statement to do
exactly the same thing, compiles successfully?

Vehicle* AnyVehicle;

if (x % 2 == 0) {
        AnyVehicle = new Car();}

else {
        AnyVehicle = new Bicycle();

}

Is it even possible to initialize these objects with the ternary
operator?

The problem is, in "left : right" part of the ternary, you have two
unrelated types as far as the compiler is concerned. It tries to
deduct a type for that part, and does not try upcasting (note that
upcasting can get pretty messy if attempted on a more complicated
derivation case). You can use static_cast<Vehicle*> on any side to
work-around. The fact that you have Vehicle on the left does not
matter, as usual conversion rules are being applied on the "="
boundary, so the right side of "=" is done in on it's own.

Goran.
 
A

Abhi

The problem is, in "left : right" part of the ternary, you have two
unrelated types as far as the compiler is concerned. It tries to
deduct a type for that part, and does not try upcasting (note that
upcasting can get pretty messy if attempted on a more complicated
derivation case). You can use static_cast<Vehicle*> on any side to
work-around. The fact that you have Vehicle on the left does not
matter, as usual conversion rules are being applied on the "="
boundary, so the right side of "=" is done in on it's own.

Goran.


Could you please explain it in bit more detail. I m still confused :(

Abhishek
 
R

Richard Damon

Hello! Suppose I have two derived classes Car and Bicycle that derive
from a base class Vehicle and an integer x. Why does the following
code give an "operand types are incompatible" error...

Vehicle* AnyVehicle = (x % 2 == 0) ? new Car() : new Bicycle();

...but the following code, which utilizes a simple if statement to do
exactly the same thing, compiles successfully?

Vehicle* AnyVehicle;

if (x % 2 == 0) {
AnyVehicle = new Car();
}
else {
AnyVehicle = new Bicycle();
}

Is it even possible to initialize these objects with the ternary
operator?

Vehicle* AnyVehicle = (x%2 == 0) ? static_cast<Vehicle*>(new Car()) :
static_cast<Vehicle*> (new Bicycle());

I think you can remove one of the static casts if you want.
 
B

Bo Persson

Abhi said:
Could you please explain it in bit more detail. I m still confused
:(

Abhishek

The compiler has to figure out the resulting type of the ternary
expression. That works fine if the two subexpressions are of the same
type, or one is convertible to the other.

That they are both convertible to a third type doesn't help.


Bo Persson
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top