vtable

Virtual tables are undefined

£可爱£侵袭症+ 提交于 2019-12-11 15:25:44
问题 I wrote some code but I am unable to compile it: #include <cstdio> #include <vector> using namespace std; class Visitor; class Land { public: virtual void accept(const Visitor *v); }; class England : public Land { public: void accept(const Visitor *v); }; class Russia : public Land { public: void accept(const Visitor *v); }; class Visitor { public: void visit(const England *e) const; void visit(const Russia *r) const; }; class Trip { private: vector<Land> *l; public: explicit Trip(vector<Land

Get the sizeof Object's Members

…衆ロ難τιáo~ 提交于 2019-12-11 12:34:47
问题 There is an object who's members I need to find the size of. I am specifically asking for the object's size without it's v-table considered. Also, I cannot modify it, so I cannot take advantage of this answer. Is there a provision for this in C++, beyond summing a hard-coded sizeof for each member? I am aware that v-tables are not mandated by C++. I am also aware that anything I do with this information will be widely considered "bad form". This question is simply asking if it's possible, not

C++ virtual destructor & vtable

时间秒杀一切 提交于 2019-12-11 06:07:30
问题 I have some specific questions on virtual destructors and vtable. Suppose I have the following code: class Base { public: virtual ~Base(); }; class Child : public Base { public: ~Child(); }; Questions: Where is the vtable stored? Is it always in the base class and all sub-classes simply keeps a pointer to it? Adding a virtual method only increases the sizeof(class) by 8 bytes right? (assume 64bit system) How about base class if it stores the table? Creating an instance of type Child via the

Offset to complete object from subobject

怎甘沉沦 提交于 2019-12-10 23:10:07
问题 I need to get the front-most address of a complete object even if what I have happens to be a subobject. The current version of my experimental smart pointer can only compare locations of a complete object and one of it's subobjects. It is simply storing their addresses and their sizes in bytes and see if they overlap. The problem lies in comparing two subobjects of a complete object in the case of multiple inheritance. Since these subobjects will not overlap each other they wont be

Why does virtual inheritance need a vtable even if no virtual functions are involved?

我们两清 提交于 2019-12-10 12:48:01
问题 I read this question: C++ Virtual class inheritance object size issue, and was wondering why virtual inheritance results in an additional vtable pointer in the class. I found an article here: https://en.wikipedia.org/wiki/Virtual_inheritance which tells us: However this offset can in the general case only be known at runtime,... I don't get what is runtime-related here. The complete class inheritance hierarchy is already known at compile time. I understand virtual functions and the use of a

C++ vtable resolving with virtual inheritance

故事扮演 提交于 2019-12-10 10:37:07
问题 I was curious about C++ and virtual inheritance - in particular, the way that vtable conflicts are resolved between bass and child classes. I won't pretend to understand the specifics on how they work, but what I've gleamed so far is that their is a small delay caused by using virtual functions due to that resolution. My question then is if the base class is blank - ie, its virtual functions are defined as: virtual void doStuff() = 0; Does this mean that the resolution is not necessary,

Workaround for lack of return type covariance when overriding virtual methods

拥有回忆 提交于 2019-12-10 02:58:21
问题 is there any way to 'hack' or 'coerce' covariant overrides in to C#? For example: public class Alpha { public virtual Alpha DoSomething() { return AlphaFactory.GetAlphaFromSomewhere(); } } public class Beta : Alpha { public override Beta DoSomething() { return BetaFactory.GetBetaFromSomewhere(); } } Unfortunately, C# doesn't support this (which seems a bit ridiculous, but that's neither here nor there). I thought I might have an answer with method hiding: new public Beta DoSomething() {

Why derived class does not have the vtable pointer and used instead vtable of the base class?

梦想的初衷 提交于 2019-12-08 07:39:00
问题 I am interested in the implementation of a virtual function in pure C. Here an example of the implementation. Then the implementation of the derived class through a pointer to the virtual functions table of the base class. Why derived class does not have the vtable pointer and used instead vtable of the base class. Maybe because they are the same offset ? void myClassDerived_ctor(struct myClassDerived *this) { myClassBase_ctor(&this->base); this->base.vtable = (void*)&myClassDerived_vtable +

Using reflection to override virtual method tables in C#

£可爱£侵袭症+ 提交于 2019-12-07 07:51:33
问题 Is there a way to change the virtual methods tables in C#? like change where a virtual method is pointing? class A { public virtual void B() { Console.WriteLine("B"); } } class Program { public static void MyB(A a) { Console.WriteLine("MyB"); } public static void Main(string[] Args) { A a = new A(); // Do some reflection voodoo to change the virtual methods table here to make B point to MyB a.B(); // Will print MyB } } 回答1: Take a look at LinFu. On Linfu's author's Blog there's an example of

Detect the the vtable offset of a specific virtual function (using Visual C++)

孤街浪徒 提交于 2019-12-06 13:43:22
问题 Can the vtable offset of a specific virtual function be inspected? Why? I'd like to be able to detect unintentional binary compatibility breaks (see http://techbase.kde.org/Policies/Binary_Compatibility_Issues_With_C%2B%2B for what I mean by binary compatibility). I'm aware of the undocumented and unsupported technique of "/d1reportSingleClassLayout" (http://blogs.msdn.com/b/vcblog/archive/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx), and I plan to use this