`static`,`extern`,`const`在头文件 [英] `static`, `extern`, `const` in header file

查看:122
本文介绍了`static`,`extern`,`const`在头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//a.h

extern int x1;
static int x2;
int x3;
static const int x4;

class A {
    public:
        static const int x5 = 10;
};

ah c $ c> .cpp 文件,我的问题是:

a.h will be included by multiple .cpp files, my question is:

1。 x1 只是一个宣言,不是吗?因此,它的定义应该在 .cpp 文件之一中完成,对吗?

1.x1 is just a declaration, isn't it? So its definition should be done in one of those .cpp files, right?

2。 x2 是一个定义,对不对?我以前认为 static int 也是一个声明,就像 extern int ,但我错了。 x2 只会显示在 ah

2.x2 is a definition, right? I used to think that static int is also a declaration just like extern int, but I was wrong. x2 will only be visible in a.h?

3如果 ah 包含在多个 .cpp 中,则 x3 c $ c>文件,因此 x3 将导致编译错误,对吗?

3.x3 will be defined multiple times if a.h is included in multiple .cpp files, so x3 will result in compile-error, right?

4。 $ c> x4 是一个定义,对吗?

4.x4 is a definition, right?

5.在A类, x5 是一个声明,是的。但是 x4

5.Here in class A, x5 is a declaration, yes. But what about x4?

推荐答案


1.x1只是一个声明,不是吗?因此,它的定义应该在那些.cpp文件中完成。

1.x1 is just a declaration, isn't it? So its definition should be done in one of those .cpp files, right?

正确


2.x2是一个定义,对吧?我曾经认为static int也是一个声明就像extern int,但我错了。 x2只会显示在啊?

2.x2 is a definition, right? I used to think that static int is also a declaration just like extern int, but I was wrong. x2 will only be visible in a.h?

不同的 x2


如果多个.cpp文件中包含ah,则会多次定义3.x3,因此x3会导致编译错误,对吗?

3.x3 will be defined multiple times if a.h is included in multiple .cpp files, so x3 will result in compile-error, right?

更准确地说,会导致链接器错误。编译器处理每个翻译单元,链接器将它们绑定在一起,并检测符号是否定义多次。

More precisely it will result in a linker error. The compiler processes each translation unit, the linker binds them together and detects that the symbol is defined multiple times.


4.x4是一个定义,right?

4.x4 is a definition, right?

是的,它是一个定义,但是 x2 每个翻译单元将有它自己的 x4 (两者都是因为 static ,因为它是 const

Yes, it is a definition, but as with x2 each translation unit will have it's own x4 (both because of static and because it is const which implies internal linkage


5.在A类中,x5是一个声明,是的,但是关于x4?

5.Here in class A, x5 is a declaration, yes. But what about x4?

是的, x5 可能会出现混淆,因为关键字 static 被重用来表示不同上下文中的不同内容。在 x5 表示类的属性,而 x4 表示内部链接

Yes, x5 is a declaration only (with initialization). The confusion might arise because the keyword static is reused to mean different things in different contexts. In x5 it means attribute of the class, while in x4 it means internal linkage

最后一种情况是特殊的,它是唯一声明(IIRC),其中声明可以有一个值,原因是它允许编译器使用包含该头部作为编译时常数的所有转换单元中的常数值。如果该值必须与定义一起提供,则只有单个翻译单元将访问该值。静态成员的定义是:

This last case is special. It is the only declaration (IIRC) where the declaration can have a value, and the reason is that it allows the compiler to use the value of the constant in all translation units that include that header as a compile time constant. If the value had to be provided with the definition, then only a single translation unit would have access to that value. The definition of that static member would be:

const int A::x5; // no initialization here

如果成员是 odr-used 。现在的事实是,在大多数情况下,常量不会被 odr使用,因为编译器将替换表达式 A :: x5 用来。只有当成员用作 lvalue 时,您需要定义,例如:

And you must provide one if the member is odr-used. Now the fact is that in most cases the constant will not be odr-used as the compiler will substitute the value when the expression A::x5 is used. Only when the member is used as an lvalue you need the definition, for example:

void f( const int & ) {}
int main() {
   f( A::x5 );
}

因为 f 是一个引用, A :: x5 的使用需要一个 lvalue (注意,const和lvalue / rvalue-正交),并且需要在程序中的单个翻译单元中定义成员。

Because the argument to f is a reference, the use of A::x5 requires an lvalue (note, const-ness and lvalue/rvalue-ness are almost orthogonal), and that requires the definition of the member in a single translation unit in your program.

这篇关于`static`,`extern`,`const`在头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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