Am 06.06.14 13:20, schrieb Alain Ketterlin:
Chris Angelico <> writes:
It's impossible to accidentally call a base class's method when you
ought to have called the overriding method in the subclass, which is a
risk in C++ [2].

I don't how this can happen in C++, unless you actually have an instance
of the base class. Anyway, I didn't mention C++.

A special, but important case of this is inside the constructor. Until you exit the constructor, C++ treats the object as not fully constructed, and if you call a virtual method there, it calls the method of the base class.

The answer is, of course, to create a *separate* init function in addition to the constructor and to require the user of the class to call it after the constructor, or to hide the real constructor away and require the user to call a factory function instead.

I love C++.
(seriously, but not /that/ part)


Reply via email to