类在顺序存储器中存储数据成员? [英] Classes store data members in sequential memory?

查看:161
本文介绍了类在顺序存储器中存储数据成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的简短版本:一个类的第一个数据成员的指针是否会产生一个指向其所有数据成员的指针?



讨论: p>

我正在读一些示例代码,这很有趣。当一个特定的函数(在这种情况下,来自OpenGL的 glUniformMatrix4fv C函数)接受一个数组作为参数时,这是通过使用指向地址的指针数组的第一个元素,如下所示:



glUniformMatrix4fv(glvariable,1,0,& newmatrix [0]); //引用newmatrix



这个开发人员有一个C ++类创建一个4X4矩阵,共有16个浮点。然而,该类的数据成员被分成4个独立的4个数据成员的向量:



以下是类的数据成员:

  vec4 x; 
vec4 y;
vec4 z; $ b $b vec4 w;

vec4 类提供这些数据成员:

  T x; 
T y;
T z;
T w;

然后创建一个函数指向第一个 vec4 x 数据成员:

  const T * Pointer()const 
{
return& ; xx;
}

不知为什么,这奇迹般的翻译成发送所有4 vec4 对象及其各自的4个组件:

  glUniformMatrix4fv(modelviewUniform,1,0,modelviewMatrix.Pointer ()); 

我是C ++的新手,但令我惊讶的是,你可以指向一个数据成员,期望所有的休息得到发送以及。是因为类按特定顺序定义它们吗?

解决方案

这是C ++对象模型。您保证在相同的访问级别( private / public / code>),对象属性是有序的(C ++ 11),对于没有访问说明符的成员(在C ++ 03中)也是如此。



这并不意味着你在做什么是个好主意。



C ++ 11:



9.2类成员[class.mem]




14)非联合类的非静态数据成员访问控制(条款11)被分配,所以
后面的成员在一个类对象内有更高的地址。未指定具有不同访问控制的非静态数据
成员的分配顺序(11)
。实现对齐要求可能
导致两个相邻成员不能彼此紧接着分配;因此可能需要
空间来管理虚拟函数(10.3)和虚拟基类(10.1)。




C ++ 03



9.2类成员[class.mem]




12)(非联合)类的非静态数据成员被分配,因此
后面的成员在类对象中具有更高的地址。未指定由访问标识符分隔的非静态数据
成员的分配顺序(11.1)
。实现对齐要求可能
导致两个相邻成员不能彼此紧接着分配;因此可能需要
空间来管理虚拟函数(10.3)和虚拟基类(10.1)。



The short version of this question: Does a pointer to the first data member of a class result in a pointer to all its data members?

Discussion:

I'm reading some sample code, and this is interesting. When a particular function (in this case the glUniformMatrix4fv C function from OpenGL) takes as a parameter an array, this is passed in the common C way of using a pointer to the address of the array's first element, as follows:

glUniformMatrix4fv(glvariable, 1, 0, &newmatrix[0]); // referring to newmatrix

This developer has a C++ class for creating a 4X4 matrix, for a total of 16 floats. However, the data members of that class are divided into 4 separate vectors of 4 data members each:

Here are the data members of the class:

vec4 x;
vec4 y;
vec4 z;
vec4 w;

The vec4 class in turn provides these data members:

T x;
T y;
T z;
T w;

He then creates a function to point to just the first vec4 x data member:

    const T* Pointer() const
{
    return &x.x;
}

And somehow, this miraculously translates into sending all 4 vec4 objects and their respective 4 components:

glUniformMatrix4fv(modelviewUniform, 1, 0, modelviewMatrix.Pointer());

I'm fairly new to C++ but this surprised me that you can point to just one data member and expect all the rest to get sent as well. Is it because the class defines them in a particular order? What if they had been defined in a different order in the class definition?

解决方案

This is the C++ object model. You're guaranteed that within the same access level (private/public/protected), the object properties are in order (C++11), and the same for members without access specifiers between them (in C++03).

This doesn't mean what you're doing is a good idea. It's better to avoid the cast and just have the function do some more work.

C++11:

9.2 Class members [class.mem]

14) Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

C++03

9.2 Class members [class.mem]

12) Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members separated by an access-specifier is unspecified (11.1). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

这篇关于类在顺序存储器中存储数据成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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