OT: This Swift thing

Christian Gollwitzer auriocus at gmx.de
Sat Jun 7 03:30:32 EDT 2014


Am 06.06.14 13:20, schrieb Alain Ketterlin:
> Chris Angelico <rosuav at gmail.com> 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.

http://www.parashift.com/c++-faq/calling-virtuals-from-ctors.html

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)

	Christian



More information about the Python-list mailing list