vtable

Dynamic method dispatching in C

落爺英雄遲暮 提交于 2019-11-27 11:58:36
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? 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 will dispatch to a read function specific to the device or service that provided that file descriptor (was it

How do virtual functions work in C# and Java?

♀尐吖头ヾ 提交于 2019-11-27 11:39:32
问题 How do the virtual functions work in C# and Java? Does it use same vtable and vpointer concept similar to C++ or is it something totally different? 回答1: How do virtual functions work in Java? Coding interviewers love this question. Yes. Although Java does NOT have a virtual keyword, Java has virtual functions and you can write them. In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a

Print address of virtual member function

主宰稳场 提交于 2019-11-27 08:34:32
I am trying to print the address of a virtual member function. If I know which class implements the function I can write: print("address: %p", &A::func); But I want to do something like this: A *b = new B(); printf("address: %p", &b->func); printf("address: %p", &b->A::func); However this does not compile. Is it possible to do something like this, perhaps looking up the address in the vtable at runtime? Lukas W Currently there is no standard way of doing this in C++ although the information must be available somewhere. Otherwise, how could the program call the function? However, GCC provides

When is a vtable created in C++?

别来无恙 提交于 2019-11-27 06:54:55
When exactly does the compiler create a virtual function table? 1) when the class contains at least one virtual function. OR 2) when the immediate base class contains at least one virtual function. OR 3) when any parent class at any level of the hierarchy contains at least one virtual function. A related question to this: Is it possible to give up dynamic dispatch in a C++ hierarchy? e.g. consider the following example. #include <iostream> using namespace std; class A { public: virtual void f(); }; class B: public A { public: void f(); }; class C: public B { public: void f(); }; Which classes

Object layout in case of virtual functions and multiple inheritance

久未见 提交于 2019-11-27 06:50:55
I was recently asked in an interview about object layout with virtual functions and multiple inheritance involved. I explained it in context of how it is implemented without multiple inheritance involved (i.e. how the compiler generated the virtual table, insert a secret pointer to the virtual table in each object and so on). It seemed to me that there was something missing in my explanation. So here are questions (see example below) What is the exact memory layout of the object of class C. Virtual tables entries for class C. Sizes (as returned by sizeof) of object of classes A, B and C. (8, 8

Virtual dispatch implementation details

我怕爱的太早我们不能终老 提交于 2019-11-27 06:25:24
First of all, I want to make myself clear that I do understand that there is no notion of vtables and vptrs in the C++ standard . However I think that virtually all implementations implement the virtual dispatch mechanism in pretty much the same way (correct me if I am wrong, but this isn't the main question). Also, I believe I know how virtual functions work , that is, I can always tell which function will be called, I just need the implementation details. Suppose someone asked me the following: "You have base class B with virtual functions v1, v2, v3 and derived class D:B which overrides

What is the VTT for a class?

最后都变了- 提交于 2019-11-27 06:07:52
Recently ran across a C++ linker error that was new to me. libfoo.so: undefined reference to `VTT for Foo' libfoo.so: undefined reference to `vtable for Foo' I recognized the error and fixed my problem, but I still have a nagging question: what exactly is a VTT? Aside: For those interested, the problem occurs when you forget to define the first virtual function declared in a class. The vtable goes into the compilation unit of the class's first virtual function. If you forget to define that function, you get a linker error that it can't find the vtable rather than the much more developer

vtable in polymorphic class of C++ using gdb [duplicate]

佐手、 提交于 2019-11-27 06:04:43
问题 This question already has answers here : Print C++ vtables using GDB (5 answers) Closed 3 years ago . How to display vtable using a pointer to base class object having virtual functions? 回答1: Did you try set print object on ? (gdb) help set print object Set printing of object's derived type based on vtable info. 回答2: If you have a sufficiently new version of gdb, you may want to look at the "info vtbl" command (or perhaps it is called "info vtable"; my own version of gdb is not sufficiently

C++ v-table: Part of the language or compiler dependent?

我的未来我决定 提交于 2019-11-27 05:59:51
问题 Is the v-table (virtual method table) a part of the C++ specification, or is it up to the compiler to solve the virtual method lookups? In case it's part of the spec: Why? I'd guess that it's compiler dependent, but someone said to me that it's part of the spec. References are very welcome! 回答1: 1.7 The C++ memory model 3 [...] Various features of the language, such as references and virtual functions, might involve additional memory locations that are not accessible to programs but are

Virtual method tables

别等时光非礼了梦想. 提交于 2019-11-27 04:04:42
问题 When discussing sealed classes, the term "virtual function table" is mentioned quite frequently. What exactly is this? I read about a method table a while ago (I don't remember the purpose of the purpose of this either) and a google/search on here brings up C++ related results. Thanks 回答1: The C# virtual function table works basically the same as the C++ one, so any resources which describe how the C++ virtual function table works should help you pretty well with the C# one as well. For