Old problem with performance

Rainer Deyke rainerd at eldwood.com
Sun Feb 8 22:55:18 PST 2009


Andrei Alexandrescu wrote:
> 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.

You're referring to the automatically generated copy constructor in
class 'avatar' which calls the copy constructor in class 'person',
right?  That's a bug in my silly untested example code, but it's not the
slicing problem.

Fix 1:

class person {
  person(person const& org, bool allow_slicing = false) {
    assert(allow_slicing || typeid(org) == typeid(*this));
  }
};

class avatar : public person, public deity {
  avatar(avatar const& org, bool allow_slicing = false)
    : person(org, true)
  {
  }
};

Fix 2:

Trust the programmer to pay attention and remove the silly unnecessary
assertion from class 'person'.


-- 
Rainer Deyke - rainerd at eldwood.com



More information about the Digitalmars-d mailing list