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


Groups > comp.lang.c++ > #7531

Re: Slicing with ?: operator - expected?

From Bart van Ingen Schenau <bart@ingen.ddns.info>
Newsgroups comp.lang.c++
Subject Re: Slicing with ?: operator - expected?
Date 2011-07-06 16:41 +0200
Organization No organisation in particular
Message-ID <iv1s6f$pde$1@localhost.localdomain> (permalink)
References <iv1p6s$acb$1@dont-email.me>

Show all headers | View raw


Victor Bazarov Wrote:

> Hello All,
> 
> Here is the code:
> 
>     class Foo {
>     public:
>        virtual void bar() const = 0;
>     };
> 
>     #include <ostream>
>     #include <iostream>
> 
>     class Foo1 : public Foo {
>        void bar() const { std::cout << "Foo1::bar\n"; }
>     };
> 
>     class Foo2 : public Foo {
>        void bar() const { std::cout << "Foo2::bar\n"; }
>     };
> 
>     void foo(const Foo* pFoo)
>     {
>        (pFoo ? *pFoo : Foo1()).bar(); // line 19 ************
>     }
> 
>     void blah(const Foo& rFoo)
>     {
>         foo(&rFoo);
>     }
> 
>     int main()
>     {
>        blah(Foo2());
>     }
> 
> Line 19 is the line in question.  Could you please interpret it for me? 
>   It seems to skip the virtual function dispatch, and attempt to call 
> the pure function.  The common type of *pFoo and Foo1() is 'class
> Foo', and the compiler seems to resolve the 'bar' statically, without 
> the use of virtual function mechanism, as if there is an instance of 
> class Foo /sliced/ from both original objects.  Is that supposed to 
> happen?

Yes, that is supposed to happen.
The conditional operator (?:) is defined to only yield rvalues, and never a
reference.
This means that objects of class-type indeed get sliced by the ?: operator.

> 
> Standard chapter and verse would be helpful.

The behaviour of the conditional operator is described in section 5.16
[expr.cond], where especially the last paragraph is of relevance here.

> 
> Thanks!
> 
> V

Bart v Ingen Schenau

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Slicing with ?: operator - expected? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-06 09:50 -0400
  Re: Slicing with ?: operator - expected? Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-06 07:41 -0700
    Re: Slicing with ?: operator - expected? Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-06 07:50 -0700
  Re: Slicing with ?: operator - expected? Bart van Ingen Schenau <bart@ingen.ddns.info> - 2011-07-06 16:41 +0200
    Re: Slicing with ?: operator - expected? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-06 11:37 -0400
      Re: Slicing with ?: operator - expected? "Balog Pal" <pasa@lib.hu> - 2011-07-06 23:17 +0200
  Re: Slicing with ?: operator - expected? Joe Greer <jgreer@doubletake.com> - 2011-07-06 21:14 +0000
    Re: Slicing with ?: operator - expected? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-08 15:56 -0400
      Re: Slicing with ?: operator - expected? Joe Greer <jgreer@doubletake.com> - 2011-07-11 16:35 +0000
        Re: Slicing with ?: operator - expected? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-11 13:05 -0400
  Re: Slicing with ?: operator - expected? "Paul" <pchristor@yahoo.co.uk> - 2011-07-07 01:47 +0100

csiph-web