如何在命名空间中使用变量 [英] How to work with variable in namespace

查看:657
本文介绍了如何在命名空间中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我对名称空间和/或静态变量有一个基本的误解.但是我已经尝试过此测试代码(手动输入,请原谅输入错误)

I think I hvae a fundamental misunderstanding of namespace and/or static variable. But I have tried this test code (typed by hand, forgive typos)

test.h:

namespace test{
   static int testNum=5;
   void setNum(int value);
}

main.cpp:

#include <test.h>

int test::setNum(int value){
   testNum=value;
}

int main(){
    test::setNum(9);
    cout<<test::testNum;
}

运行此命令时,我得到的值是5,而不是我期望的9.似乎我好像有两个testNum变量实例,但这似乎与static应该做什么完全相反.我猜我在假设这些功能与它们的Java等效项完全相同时犯了一个错误...

when I run this I get the value 5, not 9 as I would have expected. It seems almost as if I have two instances of the testNum variable, but that seems to be the exact opposite of what static should be doing. I'm guessing I've made a mistake in assuming that these features were identical to their java equvilants somehow...

如果从我的testNum声明中删除静态变量,我还会收到一条错误消息,指出将testNum声明为多次,有人可以解释为什么也是这种情况吗?

I also get an error stating that testNum is declared multuple times if I remove the static from my declaration of testNum, could someone explain why that is the case as well?

谢谢

推荐答案

首先,您的误解与命名空间无关,仅与static有关.对于此答案的其余部分,我将仅引用testNum,因为它在名称空间中的事实是无关紧要的.

First, your misunderstanding has nothing to do with namespaces, it's only about static. For the rest of this answer I'm going to refer to simply testNum because the fact it's in a namespace is irrelevant.

我还假设您还有另一个文件,可能称为test.cpp,该文件还包含test.h并定义了setNum函数.

I'm also assuming you have another file, probably called test.cpp, which also includes test.h and defines the setNum function.

当在命名空间范围内声明变量或函数(即不是类成员或函数本地变量)时,static表示该实体的名称在该文件内部.从形式上讲,它具有内部链接",这意味着它不能通过名称来引用或链接到其他文件(可以通过指针间接引用,也可以将其作为参数传递给另一个函数.)这意味着如果多个文件定义了static int testNum,那么每个文件都有自己的内部变量名称,与其他每个文件中的testNum不同(实际上,一个文件可能具有static int testnum,另一个文件可能具有static double testnum和另一个static char* testNum,如果将这样的定义放在标头中,则每个包含标头的文件都有其自己的testNum .

When a variable or function at namespace-scope (i.e. not a class member or local to a function) is declared static it means the entity's name is internal to that file. Formally it has "internal linkage", meaning it can't be referred to by name or linked to from other files (it can be indirectly referred to through a pointer or by passing it as an argument to another function.) That means if several files define static int testNum then each file has its own internal variable with that name, distinct from the testNum in every other file (in fact one file could have static int testnum and another could have static double testnum and another static char* testNum, they'd all be distinct and internal to each file.) If you put a definition like that in header then every file that includes the header has its own testNum.

因此,在标头中的变量上使用static时,每个包含test.h的文件中都有一个名为testNum不同变量.这意味着,如果在一个文件中设置testNum并在使用testNum的另一个文件中调用函数,则它引用的是不同变量,而该变量恰好具有相同的名称.

So with static on your variable in a header you have a different variable called testNum in every file that includes test.h. That means if you set testNum in one file and call a function in a different file which uses testNum it refers to a different variable, which just happens to have the same name.

因此,在标头中声明非常量static变量几乎总是错误的.

Because of this, declaring non-const static variables in headers is almost always wrong.

没有static,您将在每个包含test.h的文件中为testNum变量定义,这是不允许的:每个实体只能在程序中一次定义一次.解决此问题的方法是在标头中声明变量,而不是 define 变量,您可以通过告诉编译器变量为extern:

Without static you would have a definition of the testNum variable in every file that includes test.h, which is not allowed: every entity must be defined once and once only in your program. The way to solve that is to declare the variable in the header, but not define it, which you do by telling the compiler the variable is extern:

extern int testNum;  // N.B. no "= 1" here

这告诉编译器有一个称为外部链接"的变量,称为"testNum",因此,当代码引用testNum时,它将始终表示相同的变量(而不是带有内部linakge的名称,在每个名称中都是不同的实体声明extern变量后,您有责任确保在程序中的某个位置提供了一个完全相同的定义,因此,在完全相同的文件中(即不在包含在多个文件中的标头中)文件),您可以对其进行定义:

That tells the compiler there is a variable with "external linkage" called testNum, so when code refers to testNum it will always mean the same variable (not some name with internal linakge that is a different entity in every file.) After declaring an extern variable it is your responsibility to ensure there is exactly one definition provided somewhere in the program, so in exactly one file (i.e. not in a header that gets included in multiple files) you define it:

int testNum = 1;

这篇关于如何在命名空间中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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