Rainer Deyke wrote:
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)
  {
  }
};

So the problem exists since you are explicitly addressing it.

Fix 2:

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



So the problem exists since you are trusting the programmer to avoid it.


Andrei

Reply via email to