“静态变量"和“静态变量"之间有什么区别并在“匿名命名空间"中? [英] what's the difference between "static variable" and in "Anonymous Namespace"?

查看:87
本文介绍了“静态变量"和“静态变量"之间有什么区别并在“匿名命名空间"中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

extern int iNSIntl; //defined in other cpp
static int iNSIntl=2;
namespace{
    int iNSIntl=0;
}
void main ( int argc, char** argv )
{
    cout<<::iNSIntl<<endl;
}


在main()中,::: iNSIntl引用静态的,我如何引用外部的或在命名空间中呢?


In the main(),::iNSIntl refer to static one,how Can I refer to the extern one or within the namespace?

推荐答案

以了解该类工作的静态和非静态实例变量,请参见我过去的解决方案:

什么使静态方法可访问? [第22章-接口对象的指针在功能消失时消失使用它们是静态的. [ ^ ].

这两个解决方案中的最后一个特定于C ++.在C ++中,还存在局部静态变量这样的思想,它在与普通堆栈变量相同的上下文中声明,但在运行时与此上下文无关.它存储在专用于静态数据的区域(在某些内存模型中为进程的数据段).变量的生命周期几乎接近应用程序生命周期.当执行退出声明该变量的函数时,其值将为保留值.相反,普通的局部变量是 stack 变量:它指向栈帧内的某些内存,从逻辑上讲,该内存在调用之前甚至不存在(但实际上,它总是存在,因为在程序是为主线程加载的,并为创建时创建的每个新线程分配),因此应在每次调用时初始化局部变量.

—SA
To understand how static and non-static instance variables of the class work, please see my past solutions:

What makes static methods accessible?[^],
Catch 22 - Pointers to interface objects die when function using them is made static.[^].

Last of these two solutions is specific to C++. In C++, there is also such think as local static variable which is declared in the same context as a normal stack variable but in run-time has nothing to do with this context; it is stored in the area dedicated for static data (data segment of a process, in some memory models). The life time of the variable is nearly the lifetime of the application''s process. When the execution exits the function where such variable is declared, its value is preserver. In contrast, a normal local variable is a stack variable: it points to some memory within the stack frame which logically does not even exist before the call (but physically it always exists as the stack is allocated after the program is loaded for a main thread and allocated for each new thread created at the moment of its creation), so the local variable should be initialized on every call.

—SA


我想我理解您的问题(措词不正确).您基本上想知道全局名称空间和匿名名称空间之间的区别是什么.考虑以下示例:

I think I understand your question (which was not worded correctly). You are basically wondering what the difference between the global and anonymous namespaces are. Consider this example:

namespace
{
	void Foo()
	{
	}
}

void Foo()
{
}



现在,如果您调用Foo(),编译器将会很困惑.它们在Foo的相等可调用范围内.您将需要显式调用::Foo,后者将调用全局方法(第二个方法).您现在不能在这里呼叫匿名Foo .现在,如果您注释掉全局Foo,则对Foo 的调用(无::那里)将起作用,因为编译器在首次查找全局版本之后将在匿名名称空间中进行查找.



Now if you call Foo(), the compiler will be confused. They are both under equal callable scope for Foo. You will need to explicitly call ::Foo which calls the global method (the 2nd one). You cannot call the anonymous Foo here now. Now if you comment out the global Foo, then your call to Foo (no :: there) will work since the compiler will look in the anonymous namespace after it first looks for a global version.

Does that answer your question?


该问题无法回答,因为它不正确.这两个概念彼此无关.没事

通常,诸如"{0}和{1}之间的区别是什么"之类的问题是不正确的.你看不到吗? 苹果和苹果有什么区别?"当所比较的实体显然接近时,这种问题的意义可能非常有限.但是为什么会这样呢?

静态或非静态变量与对象的功能和运行时行为有关;它与可见性或对变量的访问无关.至于名称空间,它们仅在编译期间存在,并且仅影响名称和名称解析.编程的这两个方面永远不会发生冲突或相互影响.可以在任何名称空间或默认名称空间中声明任何非静态对象的静态对象;并且这不会影响其可访问性或行为,只会影响其命名.

您可以在任何C ++手册中找到以上所有内容的确切含义和目的.这些概念实际上是基本概念.

—SA
The question is not answerable because it is incorrect. These two notions has nothing to do with each other. Nothing at all.

Generally, the questions like "what is the difference between {0} and {1}" are not correct. Can''t you see it? "What''s the difference between apple and Apple?" Such question might have very limited sense when the compared entities are apparently close. But why would you think it''s the case?

Static or non-static variables are related to functionality and run-time behavior of objects; it has nothing to do with visibility or access to the variable. As to name spaces, they exist only during compilation and only effect names and name resolution. These two aspects of programming never clash or affect each other. Any static of non-static object can be declared in any name space or default name space; and this will not affect its accessibility or behavior, only its naming.

You can find exact meaning and purpose of all of the above in any C++ manual. These notions are really the basic ones.

—SA


这篇关于“静态变量"和“静态变量"之间有什么区别并在“匿名命名空间"中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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