简体   繁体   English

访问说明符的c ++实现

[英]c++ implementation of access specifiers

From what I understand, non-static data members in a c++ class are packed into a C-style struct.据我了解,C++ 类中的非静态数据成员被打包到 C 风格的结构中。 Ignoring virtual functions and inheritance for the sake of simplifying this discussion, how are access-specifiers enforced in such a scheme ?为了简化讨论,忽略虚函数和继承,在这样的方案中如何强制执行访问说明符?

say a class:说一个类:

class Object
    {
public:
    int i1;
    int i2;
private:
    char i3;
    int i4;
    };

translates to:翻译成:

struct { 
  int i1;
  int i2;
  char i3;
  int i4;
}

How does c++ ensure that private members i3 and i4 can not be accessed outside the class but i1 and i2 can be? c++ 如何确保私有成员i3i4不能在类外访问,而i1i2可以?

C++ has (some) safe guards to protect against Murphy, not Machiavelli. C++ 有(一些)安全措施来防止墨菲,而不是马基雅维利。

What this means is that the const , volatile and access-qualifiers are checked at compilation time , but even then can be bypassed (with a variety of tricks).这意味着constvolatile和 access-qualifiers在编译时被检查,但即使这样也可以绕过(使用各种技巧)。

So... C++ does not require implementing a protection scheme.所以... C++ 不需要实现保护方案。 If the program compiled, it is deemed correct (wrt those qualifiers) and will be executed without runtime checks.如果程序被编译,它被认为是正确的(写那些限定符)并且将在没有运行时检查的情况下执行。

It doesn't.它没有。 Go ahead, do a reinterpret_cast and manually index the pointer.继续,执行reinterpret_cast并手动索引指针。 This is for the same reason that both C and C++ allow for const to be cast away.这与 C 和 C++ 都允许丢弃const原因相同。

However, generally speaking, it's an idiotic idea to do so and C++ effectively enforces access modifiers by simply checking the modifier when you access in the normal way.然而,一般来说,这样做是一个愚蠢的想法,C++ 通过在您以正常方式访问时简单地检查修饰符来有效地强制执行访问修饰符。

C++ is a statically typed language. C++ 是一种静态类型语言。 Similarly many of the concepts in C++ are also statically checked.同样,C++ 中的许多概念也经过静态检查。 Specifically, access specifiers are a checked at compiled time.具体来说,访问说明符是在编译时检查的。

The standard gives some guarantees concerning how the members of a class are laid out in memory.该标准对类的成员在内存中的布局方式提供了一些保证。 The most useful is that members specified together in the same access specifier section will be available in the specified order.最有用的是在同一访问说明符部分中一起指定的成员将按指定的顺序可用。 (This is characteristic is quite useful for accessing network packets for example.) (例如,这个特性对于访问网络数据包非常有用。)

To answer your question, there is no "translation" to a struct (a struct is a degenerate class where the default access is public instead of private).为了回答您的问题,没有对结构的“翻译”(结构是一个退化类,其中默认访问是公共而不是私有的)。 Access specifiers are enforced at compile time.访问说明符在编译时强制执行。 There is no run time enforcement of access specifiers.访问说明符没有运行时强制执行。

What's your source?你的来源是什么? I think it refers to how the objects are represented in memory, but it's the compiler, not the run-time, that deals with access specifiers.我认为它指的是对象在内存中的表示方式,但处理访问说明符的是编译器,而不是运行时。

In memory, a class might look the same as a struct , but to the compiler they do not.在内存中,一个class可能看起来和一个struct ,但对于编译器来说它们不是。

Also, the two don't have to be equivalent even in memory (much like they are not equivalent for the compiler).此外,即使在内存中,两者也不必等效(就像它们对编译器不等效一样)。 The compiler can re-arrange the members, as long as it keeps those with no interleaving access specifiers grouped together.编译器可以重新排列成员,只要它将那些没有交错访问说明符的成员组合在一起。 So, in memory,所以,在记忆中,

class Object {  
public: 
   int i1; 
   int i2; 
private: 
   char i3; 
   int i4; 
};

could theoretically be represented as理论上可以表示为

struct {
   char i3;
   int i4;
   int i1;
   int i2;
}

Note that I'm only talking about the memory layout here.请注意,我在这里只讨论内存布局。 The actual struct equivalent of your class is:你的类的实际struct等价物是:

struct Object {  
private:
public: 
   int i1; 
   int i2; 
private: 
   char i3; 
   int i4; 
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM