It is actually impossible to deal with this problem due to the limitations of this first case.
Consider:
struct A {}; struct B : A {}; int main() { A* x = new B();
Using * makes us fail in the third case.
And for the links?
struct A {}; struct B : A {}; int main() { A& x = *(new B());
x is a reference of type A& , and it is this “entity” that has the result type.
The only way to use the first case is the direct name of the object, and we cannot do it in such a way as to hide the type of runtime:
struct A {}; struct B : A { void foo() {} }; int main() { A x = B(); // well, you've sliced it now, innit? decltype(x) y; y.foo(); // error: 'struct A' has no member named 'foo' }
That is why, according to these answers , it is always a static type of object used.
Lightness Races in Orbit Apr 09 '13 at 10:21 2013-04-09 10:21
source share