Python Learning
Christian Gollwitzer
auriocus at gmx.de
Mon Dec 18 16:27:07 EST 2017
Am 18.12.17 um 05:54 schrieb Bill:
> Chris Angelico wrote:
>> I don't know about vtables as needing to be in ANY programming course.
>> They're part of a "let's dive into the internals of C++" course. You
>> certainly don't need them to understand how things work in Python,
>> because they don't exist; and I'm doubtful that you need to explain
>> them even to C++ programmers.
>>
> Then how are you going to explain dynamic_cast?
>
You don't need to explain a vtable to explain dynamic_cast. Only if you
want to become a compiler writer. It is not even required, vtables are
just the most common implementation.
dynamic_cast simply checks if the actual object that the pointer points
to is an instance of a derived class, and then casts it into that. You
could "explain" it with the following pseudo-code
template <T*, base*>
T* dynamic_cast<T*>(base *input) {
if (isinstanceof(base, *input)) { return (T*)input; }
else { return nullptr; }
}
How that works (particularly the "isinstanceof") is not the business of
the programmer - it is sufficient to know that the compiler somewhere
stores this information for every object.
Christian
More information about the Python-list
mailing list