[英]offsetof on standard-layout class?
#include <iostream>
#include <cstddef>
class Foo
{
int a;
int b;
float c;
};
int main()
{
Foo foo;
std::cout << offsetof(Foo, b) << std::endl;
return 0;
}
The above code could not compile using gcc-4.8.2 or vc++11. 上面的代码无法使用gcc-4.8.2或vc ++ 11进行编译。 The error message is could not access private member b in class Foo.
错误消息是无法访问类Foo中的私有成员b。
But according to the standard, offsetof should support standard-layout class and Foo is a standard-layout class. 但是根据标准,offsetof应该支持标准布局类,而Foo是标准布局类。
Is this a defect of gcc-4.8.2 or vc++11, or my understanding of the c++ standard is wrong? 这是gcc-4.8.2或vc ++ 11的缺陷,还是我对c ++标准的理解是错误的?
Data members in a class are private by default . 默认情况下,类中的数据成员是私有的 。 they can be accessed from main only if it is declared under public .Or you can access them from main by declaring a function in the class under public and calling it from main.
只有在public下声明了它们,才可以从main访问它们。或者,您可以通过在public下的类中声明一个函数并从main调用它,来从main访问它们。 since the functions in the class can access the data members from the class,you can get access to the private data members.
由于类中的函数可以访问类中的数据成员,因此您可以访问私有数据成员。
offsetof is defined as a macro and therefore it can not bypass the access controls and gain access to private members , we can see that this is the case by going to draft C++ standard section 17.6.1.2
Headers paragraph 5 which says ( emphasis mine ): offsetof被定义为一个宏 ,因此它不能绕过访问控制并获得对私有成员的访问权,我们可以通过起草C ++标准第
17.6.1.2
节标题第5段来说明这种情况( 强调我的意思 ):
Names which are defined as macros in C shall be defined as macros in the C++ standard library, even if C grants license for implementation as functions.
即使C授予了实现功能的许可,在C中定义为宏的名称也应在C ++标准库中定义为宏 。 [ Note: The names defined as macros in C include the following: assert, offsetof , setjmp, va_arg, va_end, and va_start.
[注意:在C中定义为宏的名称包括:assert, offsetof ,setjmp,va_arg,va_end和va_start。 —end note ]
—尾注]
Update
更新
So there are hacks that can allow you to access the private member of a class in a standard way but if we go back to the C99 draft standard which the draft C++ standard falls back on for offsetof then we see in section 7.17
Common definitions paragraph 3 says ( emphasis mine ): 因此,有一些骇客可以允许您以标准方式访问类的私有成员,但是如果我们回到C99草案标准(C ++标准草案依赖于offsetof),那么我们将在
7.17
节“ 通用定义”第3段中看到说( 强调我的 ):
offsetof(type, member-designator)
which expands to an integer constant expression that has type size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type).
它从其结构的开头(按类型指定)扩展为一个具有size_t类型的整数常量表达式,该值的大小为以字节为单位的结构成员(由member-designator指定)的偏移量。 The type and member designator shall be such that given
类型和成员代号应使
static type t;
then the expression &(t.member-designator) evaluates to an address constant .
然后表达式&(t.member-designator)的结果为地址常量 。
which won't be the case if you are trying to access a private member from outside the class. 如果您尝试从班级外部访问私人成员 ,情况就不会如此。
class members default to private. 类成员默认为私有。 List them as public, or make Foo a struct (because struct members default to public) and the above works, else main isnt permitted to access b, and offsetof is considered an access.
将它们列出为公共,或将Foo用作结构(因为结构成员默认为public),然后进行上述工作,否则main不允许访问b,而offsetof被视为访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.