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?
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?