Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11148 > unrolled thread

Using the ternary operator to initialize derived class objects

Started byDwight Army of Champions <dwightarmyofchampions@hotmail.com>
First post2011-10-30 17:35 -0700
Last post2011-10-31 13:19 -0400
Articles 5 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  Using the ternary operator to initialize derived class objects Dwight Army of Champions <dwightarmyofchampions@hotmail.com> - 2011-10-30 17:35 -0700
    Re: Using the ternary operator to initialize derived class objects Goran <goran.pusic@gmail.com> - 2011-10-30 22:40 -0700
      Re: Using the ternary operator to initialize derived class objects Abhi <meetabhi31@gmail.com> - 2011-10-31 07:17 -0700
        Re: Using the ternary operator to initialize derived class objects "Bo Persson" <bop@gmb.dk> - 2011-10-31 18:20 +0100
    Re: Using the ternary operator to initialize derived class objects Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-31 13:19 -0400

#11148 — Using the ternary operator to initialize derived class objects

FromDwight Army of Champions <dwightarmyofchampions@hotmail.com>
Date2011-10-30 17:35 -0700
SubjectUsing the ternary operator to initialize derived class objects
Message-ID<5986ef71-bf1b-4c12-8d9b-8cb161ee1c42@f11g2000vbm.googlegroups.com>
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?

[toc] | [next] | [standalone]


#11151

FromGoran <goran.pusic@gmail.com>
Date2011-10-30 22:40 -0700
Message-ID<c6a9207b-581f-4e65-b317-ccf2fbed6a9b@gk10g2000vbb.googlegroups.com>
In reply to#11148
On Oct 31, 1:35 am, Dwight Army of Champions
<dwightarmyofchampi...@hotmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#11156

FromAbhi <meetabhi31@gmail.com>
Date2011-10-31 07:17 -0700
Message-ID<91b4a224-7d87-4b81-85a6-ecbc2036cf18@q35g2000prh.googlegroups.com>
In reply to#11151
On Oct 31, 10:40 am, Goran <goran.pu...@gmail.com> wrote:
> On Oct 31, 1:35 am, Dwight Army of Champions
>
>
>
>
>
> <dwightarmyofchampi...@hotmail.com> 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

[toc] | [prev] | [next] | [standalone]


#11161

From"Bo Persson" <bop@gmb.dk>
Date2011-10-31 18:20 +0100
Message-ID<9h83mdFbjbU1@mid.individual.net>
In reply to#11156
Abhi wrote:
> On Oct 31, 10:40 am, Goran <goran.pu...@gmail.com> wrote:
>> On Oct 31, 1:35 am, Dwight Army of Champions
>>
>>
>>
>>
>>
>> <dwightarmyofchampi...@hotmail.com> 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

[toc] | [prev] | [next] | [standalone]


#11160

FromRichard Damon <news.x.richarddamon@xoxy.net>
Date2011-10-31 13:19 -0400
Message-ID<j8mlac$j1h$1@dont-email.me>
In reply to#11148
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web