有关FAQ的第7.5节的查询 [英] queries about the FAQ's section 7.5

查看:55
本文介绍了有关FAQ的第7.5节的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我开始阅读C ++ FAQ。我无法理解章节的概念

7.5:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5


1.在C中,封装是通过在

编译单元或模块中使事物静态完成的。

我想这只是指静态函数。至于静态变量是否与b $ b相关,除了声明为extern之外,除非声明为extern,否则它们不能被除

特定翻译单元之外的任何其他地方访问,对吧?


2.顺便说一下,文件范围内的静态数据现在已经在C ++中弃用了:不要用



不明白这个!


3.不幸的是,这种方法不支持

多个数据实例,因为有没有直接支持

模块的静态数据的多个实例。你能用一个例子说明这一点

吗?


TIA

Hello all,

I ve started reading the C++ FAQ. I cannot understand the concept of section
7.5:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5

1. "In C, encapsulation was accomplished by making things static in a
compilation unit or module. "
I guess this refers only to static functions. As far as static variables are
concerned, they cannot be accessed by anywhere else apart from the
particular translation unit, unless declared as extern, right?

2. "By the way, static data at file-scope is now deprecated in C++: don''t do
that"
Cant understand this!

3. What is the meaning of "Unfortunately this approach doesn''t support
multiple instances of the data, since there is no direct support for making
multiple instances of a module''s static data." Can you illustrate this point
with an example, please?

TIA

推荐答案

jimjim写道:

/.../
jimjim wrote:
/.../
2.顺便说一下,文件范围内的静态数据现在已在C ++中弃用:不要''做那个
不能理解这个!


我的意思是,这意味着你不应该把" const int a = 1;"

在(文件范围)的文件中。相反,你应该将const int放在一个

类或结构中,并使用方法从其他地方访问该const。
3.不幸的是,这种方法没有的含义是什么?不支持
多个数据实例,因为没有直接支持制作模块的静态数据的多个实例。你能用一个例子来说明这一点吗?
2. "By the way, static data at file-scope is now deprecated in C++: don''t do
that"
Cant understand this!
My gues is that this means that you should not put e.g. "const int a=1;"
in a file at (file-scope). Rather, you should put the const int inside a
class or struct and use methods for accessing that const in from elsewhere.
3. What is the meaning of "Unfortunately this approach doesn''t support
multiple instances of the data, since there is no direct support for making
multiple instances of a module''s static data." Can you illustrate this point
with an example, please?




班级考试{

static int a;

int b;

};

static int test :: a = 1;


那是'它,你有一个*一个*的实例,无论你创建多少个对象

的类测试,观察每个新的测试实例将

有新的b'的。


问候,

Peter Jansson



class test {
static int a;
int b;
};
static int test::a=1;

That''s it, you have *one* instance of a, regardless of how many objects
of class test you create, observe that each new instance of test will
have new b''s.

Regards,
Peter Jansson


* jimjim:
大家好,

我开始阅读C ++ FAQ。我无法理解第7.5节的概念:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5

1。 在C中,封装是通过在编译单元或模块中使事物静止来完成的。
我想这只涉及静态功能。至于静态变量是关注的,除非声明为extern,否则它们不能被除特定翻译单元以外的任何其他地方访问,对吧?

2。顺便说一句,文件范围内的静态数据现在已经在C ++中弃用了:不要这样做
不能理解这一点!
Hello all,

I ve started reading the C++ FAQ. I cannot understand the concept of section
7.5:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5

1. "In C, encapsulation was accomplished by making things static in a
compilation unit or module. "
I guess this refers only to static functions. As far as static variables are
concerned, they cannot be accessed by anywhere else apart from the
particular translation unit, unless declared as extern, right?

2. "By the way, static data at file-scope is now deprecated in C++: don''t do
that"
Cant understand this!




这意味着代替C风格


static int x = 666;

static void foo(){}


你应该优先使用C ++风格的匿名命名空间,


命名空间

{

int x = 666;

void foo();

}


我们不会问为什么。但是你可以使用模板机制的匿名命名空间版本

foo。你不能用静态版本做到这一点。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和e-最令人烦恼的是什么?邮件?



It means that instead of C style

static int x = 666;
static void foo() {}

you should preferentially use a C++ style anonymous namespace,

namespace
{
int x = 666;
void foo();
}

Ours is not to ask why. But you can use the anonymous namespace version of
foo with the template mechanism. You cannot do that with the static version.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


jimjim写道:
大家好,

我开始阅读C ++ FAQ。我无法理解第7.5节的概念:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5

1。 在C中,封装是通过在编译单元或模块中使事物静止来完成的。
我想这只涉及静态功能。就静态变量而言,除了声明为extern之外,它们不能被除特定翻译单元之外的任何其他地方访问,对吧?


好​​吧,变量不能同时声明为extern和static。你有没有原则,但是:静态允许C封装

实现其公开可用(即外部声明)

接口。

2.顺便说一句,文件范围内的静态数据现在已经在C ++中弃用了:不要这样做
不能理解这一点!
Hello all,

I ve started reading the C++ FAQ. I cannot understand the concept of section
7.5:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5

1. "In C, encapsulation was accomplished by making things static in a
compilation unit or module. "
I guess this refers only to static functions. As far as static variables are
concerned, they cannot be accessed by anywhere else apart from the
particular translation unit, unless declared as extern, right?
Well, variables can''t be declared extern AND static simultaneously. You
have the principle right, however: static allows C to encapsulate
implementation behind its publicly available (i.e., extern-declared)
interface.
2. "By the way, static data at file-scope is now deprecated in C++: don''t do
that"
Cant understand this!




Quoustrup:在C ++中不推荐使用静态来表示''本地的

翻译单元''。使用未命名的命名空间代替

(第8.2.5.1节) (C ++ PL,B.2.3节)。


换句话说,不要这样做:


//文件。 cpp

static int i = 0;


相反,请执行以下操作:


// File.cpp < br $>
命名空间{

int i = 0;

}


未命名的命名空间有一个隐含的using指令并且在翻译单位之间是唯一的




干杯! --M



Quoth the Stroustrup: "The use of static to indicate ''local to a
translation unit'' is deprecated in C++. Use unnamed namespaces instead
(section 8.2.5.1)" (C++PL, section B.2.3).

In other words don''t do this:

// File.cpp
static int i = 0;

Instead, do this:

// File.cpp
namespace {
int i = 0;
}

Unnamed namespaces have an implicit using directive and are unique
between translation units.

Cheers! --M


这篇关于有关FAQ的第7.5节的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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