虚拟方法表在C ++中存储在哪里? [英] Where does virtual method table store in C++?

查看:186
本文介绍了虚拟方法表在C ++中存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道类对象(不是实例,而是类)存储在内存中。

  
public:
int a;
virtual void f();
virtual〜A();
};

class B:public A {
public:
int b;
void f()final override;
};

我知道,通常情况下(没有强烈描述的标准)在这种继承的情况下A)我们有:

  memory:.... AB ... 

其中 AB 是B的类对象(如果我正确理解)。
如果我们更深入(尝试使用 clang gcc ),我们可以看到类似的东西(再次,在标准中没有强烈描述):

  A 
vtptr *
int a
B
vtptr *
int b

好的,现在我们看到 a b 属性存储。我们还可以看到指针到虚拟方法表。但是 vtptr * (虚拟方法表)实际存储在哪里?为什么不靠近类?或者它吗?



另外,这里还有一个问题:我可以通过更改指针(简单逻辑)来更改虚拟方法表。我还可以安全地更改指向它的方法的指针吗?



在你的问题,你可以回答gcc和铛。
P.P.S.

解决方案

C ++标准没有规定虚函数机制应该如何实施。实际上,所有的C ++实现使用每个类的虚函数表,以及在具有虚函数(称为多态类)的类的每个对象中的虚函数表指针。



您可以阅读Stanley Lippman的经典书




为什么不接近课程?


< blockquote>

…类本身不存储在任何地方,它们不是对象,所以这没有意义,对不起。



你可以更有意义地询问vtable指针存储在哪里每个对象,对于给定的实现?



通常是在对象的开始,但如果从非多态类派生,并添加一个虚函数,那么你可能得到vtable指针在别的地方。或不。后面的可能性是为什么 static_cast Derived * Base * / code>(反之亦然)可以进行地址调整,即不同于简单的 reinterpret_cast


I want to know how does class object (not instances, but exactly classes) store in memory?

class A {
public:
    int a;
    virtual void f();
    virtual ~A();
};

class B : public A {
public:
    int b;
    void f() final override;
};

I know, that usually (not strongly described by standard) in case of this inheritance (B derived from A) we have:

memory: ....AB...

where AB is a class object of B (if I understand it correctly). If we go deeper (tried with clang and gcc), we can see something like (again, not strongly described in standard):

A
    vtptr*
    int a
B
    vtptr*
    int b

Okay, now we see where do the a and b properties store. And we also see the pointer to virtual method table. But where does vtptr* (virtual method table) actually store? Why not near with the classes? Or it does?

Also, here is another question: I was able to change virtual method tables by changing the pointers (simple logic). Can I also change a pointer to it's methods safely?

P.S. In your questions you may answer for gcc and clang. P.P.S. If I am wrong somewhere please point it too in your answers.

解决方案

The C++ standard does not prescribe how the virtual function mechanism should be implemented. In practice all C++ implementations use a virtual function table per class, and a virtual function table pointer in each object of class with virtual functions (called a polymorphic class). Yet the details can differ, in particular for multiple inheritance and virtual inheritance.

You can read about the common choices in Stanley Lippman's classic book Inside The C++ Object Model.

It doesn't make much sense to ask “where” a virtual function table is stored. It's much like any static variable: its location depends on the implementation and is pretty much arbitrary. And regarding

Why not near with the classes?

… classes as such are not stored anywhere, they are not objects, so this doesn't make sense, sorry.

You can ask more meaningfully where is the vtable pointer stored in each object, for a given implementation?

And usually that's at the start of the object, but if you derive from a non-polymorphic class, and add a virtual function, then you might get the vtable pointer somewhere else. Or not. The latter possibility is much of the reason why a static_cast of Derived* to Base* (or vice versa) can do an address adjustment, i.e. is different from a simple reinterpret_cast.

这篇关于虚拟方法表在C ++中存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆