Old problem with performance

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Feb 8 22:05:48 PST 2009


Rainer Deyke wrote:
> Example 3 (C++):
> 
> class person {
> public:
>   person(person const& org) {
>     assert(typeid(org) == typeid(*this));
>   }
>   virtual ~person() {}
> };
> 
> class avatar : public person, public deity {
> };
> 
> 'person' and 'avatar' are value types that can be used polymorphically.
>  In any context 'person&' may be either a reference to an instance of
> class 'person' or a polymorphic reference to an instance of any class
> that inherits from 'person'.  Unfortunately the C++ type system does not
> distinguish between these two uses.  It is an error (checked at runtime)
> to construct a new 'person' from a polymorphic reference, but any other
> use of 'person' as a value type or a polymorphic type is valid.  No
> slicing problem exists except through user error.

The slicing problem exists in spades in this example, or better put its 
converse (your code will fire asserts when it shouldn't). The reason is 
rather subtle, so please try the code out to edify yourself.

Andrei



More information about the Digitalmars-d mailing list