Using the ternary operator to initialize derived class objects

Discussion in 'C++' started by Dwight Army of Champions, Oct 31, 2011.

  1. 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?
     
    Dwight Army of Champions, Oct 31, 2011
    #1
    1. Advertising

  2. Dwight Army of Champions

    Goran Guest

    On Oct 31, 1:35 am, Dwight Army of Champions
    <> wrote:
    > 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.
     
    Goran, Oct 31, 2011
    #2
    1. Advertising

  3. Dwight Army of Champions

    Abhi Guest

    On Oct 31, 10:40 am, Goran <> wrote:
    > On Oct 31, 1:35 am, Dwight Army of Champions
    >
    >
    >
    >
    >
    > <> wrote:
    > > 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.



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

    Abhishek
     
    Abhi, Oct 31, 2011
    #3
  4. On 10/30/11 8:35 PM, Dwight Army of Champions wrote:
    > 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.
     
    Richard Damon, Oct 31, 2011
    #4
  5. Dwight Army of Champions

    Bo Persson Guest

    Abhi wrote:
    > On Oct 31, 10:40 am, Goran <> wrote:
    >> On Oct 31, 1:35 am, Dwight Army of Champions
    >>
    >>
    >>
    >>
    >>
    >> <> wrote:
    >>> 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.

    >
    >
    > 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
     
    Bo Persson, Oct 31, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacob

    ternary operator error

    Jacob, Jun 30, 2003, in forum: Java
    Replies:
    12
    Views:
    4,777
    pete kirkham
    Jul 2, 2003
  2. Roger Leigh

    ternary operator and ostreams

    Roger Leigh, Jan 16, 2004, in forum: C++
    Replies:
    6
    Views:
    680
    Roger Leigh
    Jan 19, 2004
  3. marco_segurini
    Replies:
    4
    Views:
    830
    Dan Cernat
    Sep 21, 2004
  4. Replies:
    1
    Views:
    422
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    408
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page