+ 2
What is inherited?
3 ответов
+ 8
Dynamic inheritance should mean that you can alter the class hierarchy at runtime.
This is something which should be pretty straightforward to do in a dynamic language. For instance, in Javascript, an object will have a prototype property. If you try to access a method or property not defined for that object, it will be looked up in the prototype object, and so on. If you change the prototype, you're effectively changing what the object is and can do. See here and on Wikipedia for a more sound description.
As for how you could implement this in C++, it would probably be pretty messy. For a simplified example:
class A
{ public:
/*some fields*/
virtual ~A() { }
virtual void f() = 0;
virtual void g() = 0; };
class B1 : public A
{ public:
virtual void f()
{ /*B1 implementation*/ }
virtual void g()
{ /*B1 implementation*/ } };
class B2 : public A
{ public:
virtual void f()
{ /*B2 implementation*/ }
virtual void g()
{ /*B2 implementation*/ } };
class C : public B1
{ public: /* ... */ };
you should be able to simply (although in a compiler-specific way) switch the vptrin a C object to point to the v-table for B2at runtime, and you'd have your Cbehaving as if its class inherited from B2. This approach can get messy really quickly if we bring in object layouts, and possibly automatic inlining - say, if the compiler thinks it knows what the dynamic type of the object is and doesn't bother with the vtable.
To do it properly in C++, my guess is you might need to resort to some sort of table system, perhaps using metatables as in Lua, and then you'd be throwing away all manners of comfortable syntax. Or you'd end up with a scripting language. In a certain sense, since most dynamic language interpreters are written in C or C++, you could argue that they add dynamic inheritance support to C++.
Hope this helps!
+ 2
inherit text is following by the specification of the previous word
+ 1
Do you mean.... Inheritance?