且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++对象模型布局

更新时间:2023-12-02 22:31:34

sumsin写道:
sumsin wrote:

C ++对象模型书说

''非静态数据会员直接在每个班级内分配

对象。静态数据成员存储在单个类

对象之外。静态和非静态函数成员也在类对象外面提升。虚函数分两步支持:


为每个类生成一个指向虚函数的指针表

(这称为虚拟表)。 br />

每个类对象都插入一个指向相关虚拟表的指针(传统上,这被称为vptr)。通过在每个类构造函数中生成的代码,

析构函数和副本自动处理

设置,重置和不设置vptr

赋值运算符(这将在章节

5中讨论)。与支持

运行时类型标识(RTTI)的每个类关联的type_info对象也在

虚拟表中寻址,通常在表的第一个插槽中。 ''
The C++ Object Model book says that
''Nonstatic data members are allocated directly within each class
object. Static data members are stored outside the individual class
object. Static and nonstatic function members are also hoisted outside
the class object. Virtual functions are supported in two steps:

A table of pointers to virtual functions is generated for each class
(this is called the virtual table).

A single pointer to the associated virtual table is inserted within
each class object (traditionally, this has been called the vptr). The
setting, resetting, and not setting of the vptr is handled
automatically through code generated within each class constructor,
destructor, and copy assignment operator (this is discussed in Chapter
5). The type_info object associated with each class in support of
runtime type identification (RTTI) is also addressed within the
virtual table, usually within the table''s first slot.''



这是一种方法。我不认为标准是关于

如何实现它的具体标准。但是这是一个常见的方法。

That''s a way to do it. I don''t think the standard is that specific about how
to implement it. But it is a common aproach yes.


在这里我理解一个人有一些方法可以访问:

- 非静态数据成员和

- 类对象的虚函数。


然后如何访问其他实体,如:

- 静态数据会员

- 静态和非静态成员函数?
Here I understand that one have some means to access:
- non-static data members and
- virtual function of the class object.

But then how one can access the other entities like:
- static data member
- static and non-static member functions?



回答这个问题:如何访问全局变量或命名空间范围

函数(用C或C ++)?


当你回答这个问题时,你可以将它应用到你的原始问题中,如果你想象静态数据成员只是一些全局变量,那么

a"特别名称 (即struct A {static int a;};定义一个globalA :: a),

静态成员函数是相同的(即struct A {static void func()

{}};定义全局A :: func())和非静态成员函数

类似于静态函数,只是它们具有隐藏的this和this。参数,

so struct A {void func(){}};基本上就像有结构A {静态

void func(A * this){}};

Answer this question: how one can access global variables or namespace scope
functions (in C or C++)?

When you answer that you can apply it to your original questions if you
imagine that static data member are just some global variables with
a "special name" (ie struct A { static int a; }; defines a "global" A::a),
static member functions are the same (ie struct A { static void func()
{} }; defines a "global" A::func()) and non static member functions are
similar to static functions only that they have a hidden "this" parameter,
so struct A { void func() {} }; is basically like having struct A { static
void func(A* this) {} };


如何用上面的实体表示尊重班级对象

布局?
How the above entities are represented with respect to class object
layout?



他们与对象布局没什么共同之处。


-

Dizzy

They don''t have much in common with the object layout.

--
Dizzy


sumsin写道:
sumsin wrote:

C ++对象模型书中说

''非静态数据成员直接在每个类中分配

对象。静态数据成员存储在单个类

对象之外。静态和非静态函数成员也在类对象外面提升。虚函数分两步支持:


为每个类生成一个指向虚函数的指针表

(这称为虚拟表)。 br />

每个类对象都插入一个指向相关虚拟表的指针(传统上,这被称为vptr)。通过在每个类构造函数中生成的代码,

析构函数和副本自动处理

设置,重置和不设置vptr

赋值运算符(这将在章节

5中讨论)。与支持

运行时类型标识(RTTI)的每个类关联的type_info对象也在

虚拟表中寻址,通常在表的第一个插槽中。 ''


我知道有一些方法可以访问:

- 非静态数据成员和

- 虚拟类对象的功能。


然后如何访问其他实体如:

- 静态数据成员

- 静态和非静态成员函数?
The C++ Object Model book says that
''Nonstatic data members are allocated directly within each class
object. Static data members are stored outside the individual class
object. Static and nonstatic function members are also hoisted outside
the class object. Virtual functions are supported in two steps:

A table of pointers to virtual functions is generated for each class
(this is called the virtual table).

A single pointer to the associated virtual table is inserted within
each class object (traditionally, this has been called the vptr). The
setting, resetting, and not setting of the vptr is handled
automatically through code generated within each class constructor,
destructor, and copy assignment operator (this is discussed in Chapter
5). The type_info object associated with each class in support of
runtime type identification (RTTI) is also addressed within the
virtual table, usually within the table''s first slot.''

Here I understand that one have some means to access:
- non-static data members and
- virtual function of the class object.

But then how one can access the other entities like:
- static data member
- static and non-static member functions?



通常语法是<类的名称::<成员名称>,例如


MyClass :: myStaticMember





MyClass :: myStaticFunction(参数)


你也是可以使用对象和引用的点符号和

如果你想要的话,指向对象的箭头符号:


MyClass myObject;

MyClass * ptr =& myObject;

... myObject.myStaticMember ...

... ptr-&gt ; myStaticFunction(arguments)...

Usually the syntax is <name of the class:: <name of the member>, like

MyClass::myStaticMember

or

MyClass::myStaticFunction(arguments)

And you also can use the ''dot'' notation for objects and references and
the ''arrow'' notation for pointers to objects if you want to:

MyClass myObject;
MyClass *ptr = &myObject;
... myObject.myStaticMember ...
... ptr->myStaticFunction(arguments) ...


>

如何根据类对象表示上述实体

布局?
>
How the above entities are represented with respect to class object
layout?



常规函数和非静态成员之间的区别

函数是后者具有指定

为其调用的对象。在非静态成员内部

函数表达式''this''(一个不可修改的左值)给出了

指向成员函数的对象的指针调用。其他

,功能基本上都是一样的。


静态数据成员本质上是全局数据。唯一的区别是

类成员有访问说明符(常规全局数据不是
),但这只是编译时功能,如果它在运行期间以任何方式强制执行(一旦它编译,没有隐私或

其他机制存在WRT成员与非成员相比)。


引用自己的文字,

The difference between a regular function and a non-static member
function is that the latter has the hidden argument that designates the
object for which it''s being called. Inside the non-static member
function the expression ''this'' (a non-modifiable lvalue) gives you the
pointer to the object for which the member function is called. Other
than that, the functions are basically all the same.

Static data members are essentially global data. The only difference is
that class members have access specifiers (which regular global data
don''t), but this is "only the compile-time feature", IOW it''s not
enforced in any way during run-time (once it compiles, no privacy or
other mechanism exists WRT members compared to non-members).

To quote your own text,


静态数据成员存储在个别班级之外

对象。
Static data members are stored outside the individual class
object.



这意味着类对象中没有静态数据。


V

- -

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

That means there is no static data in the class object.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


文章< 4af52a26-03b6-48f2-b348-82edf5490ba7

@ v1g2000pra.googlegroups.com>, su ******* @ gmail.com 说...


[...]
In article <4af52a26-03b6-48f2-b348-82edf5490ba7
@v1g2000pra.googlegroups.com>, su*******@gmail.com says...

[ ... ]

然后如何访问其他实体,例如:

- 静态数据成员
But then how one can access the other entities like:
- static data member



静态数据成员只是一个有趣名称的全局变量。它只是链接器分配了一个地址,并且引用它的所有内容

都使用该地址。

A static data member is just a global variable with a funny name. It''s
just assigned an address by the linker, and everything that refers to it
uses that address.


- 静态和非静态成员函数?
- static and non-static member functions?



静态成员函数只是普通的全局函数,有趣的

名称 - 你可能有:


class my_class {

public:

static_func(){}

};


,编译器将其转换为如下名称:


static_func

Static member functions are just normal global functions with funny
names -- you might have:

class my_class {
public:
static_func() {}
};

and the compiler turns that into a name like:

static_func