English 中文(简体)
名称空间是否与C++的范围相同?
原标题:Is a namespace the same thing as scope in C++?
  • 时间:2011-11-20 13:06:54
  •  标签:
  • c++

Is a namespace the same thing as scope in C++ programming language? Are these two words synonyms and can be used interchangably?

最佳回答

页: 1 名称空间是一种范围,但范围有若干种,是国名空间。 一些实例:

  • block scope: symbols (usually local variables) declared within a statement block enclosed in {...};
  • function scope: a function s outermost block scope;
  • class scope: the members of a class.
问题回答

并非完全。

In C++, a scope is generally determined by pair of opening and closing braces: { and }, this includes:

  • namespaces
  • classes and structs
  • functions and methods
  • simple "blocks" within functions or methods

然而,一些声明有少数例外或特殊性,这些声明可能会带来一些变量,这些变量将在其眼前的范围之内:

for (int i = 0; i < 10; ++i) {
  // i accessible here
}

while (int c = getchar()) {
  // c accessible here
}

try {
  // ...
} catch(std::exception const& e) {
  // e accessible here
}

void foo(int i) {
  // i accessible here
}

并且有最令人惊讶的<代码>。

if (int var = /**/)
{
  // var is accessible here
}
else
{
  // var is accessible here too!
}

这些范围是:

由于C规则(C++也遵循)使<代码>{<>/代码>和<代码>>>>>>>>>>>>>在表述中选取:

for (int i = 0; i < 10; ++i) std::cout << i << "
";
// i no longer accessible here

模板参数也有其范围:这些参数在申报点活到生命,直到其课目或职能结束。

template <typename U>
void foo() {
} // U accessible up until this point

最后,还有“最外部”的范围,即全球名称空间,即“可支配”范围。

没有。

A scope is pretty much similar to an object s lifetime - similar, not the same.

class A;

//....
{
   A a;  //begin scope for object a
}
//object a is out of scope

A namespace is a useful way to group declarations and definitions in C++.

你们可以指定一个名称空间,但范围远大于此。 见范围解决操作者:一种方法或变量可以是类别范围的一部分,而不是在名称空间。

Wikipedia s articles on Scope &Variables 应当有所帮助。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签