C ++中的访问说明符 [英] Access Specifier in C++

查看:116
本文介绍了C ++中的访问说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从BRUCE ECKEL的C ++语言中读到以下语句

I read the following statements from BRUCE ECKEL'S THINKING IN C++


1.访问说明符是结构的一部分,
从结构创建

1.Access specifier are part of structure and do not affect the object
created from "structure

疑问:由于我们知道访问块不连续存储,$ b $ a b

Doubt:As we know the access blocks are not stored contiguously, aint it that access specifier change the way object layout in memory


2.所有访问规范信息在程序运行之前消失

2.All of the access specification information disappear before the program is run
(during compilation).In a running program,object become the "region of storage"
and nothing more..thus we can break all the rules and access the memory directly
as you can in c

疑问:这是否意味着可以直接访问私人会员?理解上述语句

Doubt:Does it mean one can even access private member directly?please help me to comprehend the above statement


3.c ++的设计是务实的,不要求抽象交易

3.c++ is design to be pragmatic, not to aspire to abstract deal

疑问:什么是务实的意思?

Doubt:whats a meaning of being pragmatic?

推荐答案


1)访问说明符是结构的一部分,不影响从结构创建的对象

1) Access specifier are part of structure and do not affect the object created from "structure"

相同访问特定( public protected 或<$ c $)中数据成员的顺序c> private )由它们在代码中的顺序决定,但没有为具有不同说明符的数据成员指定顺序。

Wrong actually, the order (in the layout) of data members within the same access specific (public, protected or private) is dictated by their order in the code, however no order is specified for data members with different specifiers.

class Foo
{
public:
  int a;
  int b;
protected:
  int c;
  int d;
};

我们知道的唯一的事情是 a 必须在 b 之前, c 必须在 d 之前。 abcd acbd acdb cabd cadb cdab 都是可能的。

The only thing we know is that a must come before b and c must come before d. abcd, acbd, acdb, cabd, cadb and cdab are all possible.


2)所有访问规范信息在程序运行之前消失
(在编译期间)。在正在运行的程序中,对象成为
和没有更多..因此我们可以打破所有规则和直接访问内存
,你可以在c

2) All of the access specification information disappear before the program is run (during compilation).In a running program,object become the "region of storage" and nothing more.. thus we can break all the rules and access the memory directly as you can in c

信息仅在编译期间使用,但编译是生成运行代码的代码。因此,编译确保您不会访问 private 成员。然而,由于允许直接的内存操作,你可以有效地访问 private 成员或函数,如果你愿意,它只是高度容易错误尝试和手动。 >

The information is only used during compilation, but then compilation is what generate the running code. Therefore compilation ensures that you won't access private members. However, since direct memory manipulation is permitted, you could effectively access private members or functions if you wish, it's just highly error-prone to try and do it manually.


3)C ++旨在务实,而不是渴望抽象真实的

3) C++ is designed to be pragmatic, not to aspire to abstract the real

务实意味着它适合实际使用,几乎不考虑纯粹的理论论证。从CS理论构建的语言将包括例如Haskell,其具有非常可靠的数学背景;另一方面C ++已经积累了被用户认为有用的功能。

Pragmatic means that it's geared toward real use, with little consideration for purely theoretic arguments. Languages built from CS theory would include Haskell for example, which has an extremely sound mathematical background; on the other hand C++ has accumulated features that were deemed useful by its users.

此外,C ++不会隐藏底层的细节(像内存管理)。好的C ++代码通常留给编译器,并使用习语尝试和抽象它(有点),但如果必要,你可以总是更接近金属(甚至包括汇编代码直接)...有时(像内存管理)你必须注意你在做什么。

Also, C++ does not hide the low-level details from you (like memory management). Good C++ code generally leave it up to the compiler and use idioms to try and abstract it (somewhat), but if necessary you can always get closer to the metal (even including Assembly code directly)... and sometimes (like memory management) you do have to pay attention to what you're doing.

这篇关于C ++中的访问说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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