vtable

Dynamic method dispatching in C

半世苍凉 提交于 2019-12-28 03:38:14
问题 I know it sounds silly and I know that C is not an Object Oriented Language. But is there any way that dynamic method dispatching can be achieved in C? I thought about function pointers but don't get the entire idea. How could I implement this? 回答1: As others have noted, it is certainly possible to implement this in C. Not only is it possible, it is a fairly common mechanism. The most commonly used example is probably the file descriptor interface in UNIX. A read() call on a file descriptor

On VTable pointers and malloc

半腔热情 提交于 2019-12-25 06:52:24
问题 Is there any compiler independent and syntactically elegant way to set a vtable pointer in an object allocated with malloc? I cannot use new directly as I need to be able to control the flow of memory releases on demand which requires the use of void ptrs to hold memory locations in a memory manager until there is ample time to release. class AbstractData { public: AbstractData() {} virtual ~AbstractData() {} protected: virtual void SetData(int NewData) =0; virtual int GetData() const =0; };

How to get function name against function address by reading co-classs'es vtable?

故事扮演 提交于 2019-12-25 04:42:33
问题 I need to call the co-class function by reading its address from vtable of COM exposed interface methods. I need some generic way to read addresses. Now I need to call the function, which would have specific address(NOT KNOWN) arguments(parameters) which I have collected from TLB, and name as well. How that address corresponds to that function name to which I am going to call. For this I need to traverse vtable which is holding functional addresses, LASTLY need to correspond function address

Eclipse C++ project not building: Constructor Destructor Issue

北城余情 提交于 2019-12-24 01:36:33
问题 I have all the class definitions in a header file: ModelModule.h. I have provided the sample code for that file below where I have given the declaration of 2 classes and its member functions: #pragma once #if !defined( MODELMODULE_H ) #define MODELMODULE_H //Required header files class CModelModule; class COrdProbitMM; class CModelModule // virtual base class for all types of modeling modules { friend class CSimCoordinator; friend class CHouseholdCoordinator; friend class CGenericHousehold;

Low level details of inheritance and polymorphism

*爱你&永不变心* 提交于 2019-12-22 05:15:13
问题 This question is one of the big doubts that looms around my head and is also hard to describe it in terms of words . Some times it seems obvious and sometimes a tough one to crack.So the question goes like this:: class Base{ public: int a_number; Base(){} virtual void function1() {} virtual void function2() {} void function3() {} }; class Derived:public Base{ public: Derived():Base() {} void function1() {cout &lt&lt "Derived from Base" &lt&lt endl; virtual void function4() {cout &lt&lt "Only

How to use the vtable to determine class type

家住魔仙堡 提交于 2019-12-22 04:39:06
问题 I was recently on an interview for a position where C/C++ is the primary language and during one question I was told that it's possible to use the vtable to determine which class in a hierarchy a base pointer actually stores. So if, for example you have class A { public: A() {} virtual ~A() {} virtual void method1() {} }; class B : public A { public: B() {} virtual ~B() {} virtual void method1() {} }; and you instantiate A * pFoo = new B() , is it indeed possible to use the vtable to

Virtual inheritance and empty vtable in base class

梦想的初衷 提交于 2019-12-21 06:09:30
问题 There is this code: #include <iostream> class Base { int x; }; class Derived : virtual public Base { int y; }; int main() { std::cout << sizeof(Derived) << std::endl; // prints 12 return 0; } I have read that when some class is virtually inherited then there is created empty vtable for class Derived, so memory layout is as follows: Derived::ptr to empty vtable Derived::y Base::x and it is 12 bytes. The question is - what is purpose of this empty vtable if there are not any virtual methods and

understanding vptr in multiple inheritance?

大兔子大兔子 提交于 2019-12-20 08:38:56
问题 I am trying to make sense of the statement in book effective c++. Following is the inheritance diagram for multiple inheritance. Now the book says separate memory in each class is required for vptr. Also it makes following statement An oddity in the above diagram is that there are only three vptrs even though four classes are involved. Implementations are free to generate four vptrs if they like, but three suffice (it turns out that B and D can share a vptr), and most implementations take

How Vtable of Virtual functions work

点点圈 提交于 2019-12-19 08:54:51
问题 I have a small doubt in Virtual Table, whenever compiler encounters the virtual functions in a class, it creates Vtable and places virtual functions address over there. It happens similarly for other class which inherits. Does it create a new pointer in each class which points to each Vtable? If not how does it access the Virtual function when the new instance of derived class is created and assigned to Base PTR? 回答1: Each time you create a class that contains virtual functions, or you derive

COM method offsets in Delphi

人走茶凉 提交于 2019-12-18 15:58:07
问题 In Delphi, how do I find out the the address of a COM method? I can hardcode the offsets //0 is the offset of the QueryInterface method p := TPonterArray(pointer(SomeInterface)^)[0]; but I would prefer to use symbolic names. The folllowing obviously does not work: var M : TMethod; ... M := TMethod(SomeInterface.QueryInterface); Thanks! 回答1: You can use the vmtoffset assembler directive to get the byte offset of an interface method relative to the start of the interface's method table. Take a