多个文件中的常量定义

drd*_*dot 12 c++ const

我正在阅读"C++ primer plus".在第9章中,它讨论了处理const修饰符时C++和C之间的区别:

"在C++(但不是C)中,const修饰符稍微改变了默认存储类.默认情况下,全局变量具有外部链接,而const全局变量默认具有内部链接.

...

如果全局const声明具有外部链接作为常规变量do,则这将是一个错误,因为您只能在一个文件中定义全局变量.也就是说,只有一个文件可以包含程序声明,其他文件必须使用extern关键字提供引用声明."

我尝试使用以下程序测试此声明:

file.h:

using namespace std;

const char *constant = "Magic";
Run Code Online (Sandbox Code Playgroud)

file1.cpp

#include <iostream>
#include "file.h"
extern void file2();
int main(){
  cout << "constant = " << constant << endl;
  file2();
}
Run Code Online (Sandbox Code Playgroud)

file2.cpp

#include <iostream>
#include "file.h"

void file2(){
  cout << "file2 constant = " << constant << endl;
}
Run Code Online (Sandbox Code Playgroud)

Makefile文件:

CFLAGS = -Wall -g
INCLUDE = file.h
src = file2.cpp file1.cpp

all: $(src) $(INCLUDE)
  g++ $(CFLAGS) -o file $(src)

clean:
  rm -f file
Run Code Online (Sandbox Code Playgroud)

当我做,我收到以下错误信息:

g++ -Wall -g -o file file2.cpp file1.cpp
/tmp/ccdl16Tw.o:(.data+0x0): multiple definition of `constant'
/tmp/ccA3ZEHa.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
Run Code Online (Sandbox Code Playgroud)

gcc版本4.8.2

更新:

如果我做

 char* const constant = "Magic";
Run Code Online (Sandbox Code Playgroud)

然后make会给我这个警告:

g ++ -Wall -g -o file file2.cpp file1.cpp

In file included from file2.cpp:2:0:
file.h:3:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
 char* const constant = "Magic";
                        ^
In file included from file1.cpp:2:0:
file.h:3:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
 char* const constant = "Magic";
Run Code Online (Sandbox Code Playgroud)

eer*_*ika 13

const char *constant不是const.它是一个非const指针const char.const在命名空间范围内是非变量的,默认情况下它具有外部链接,因此您会得到多个定义错误.

const char * const constantconst和你会表现得像你期待的那样.

更新:

char* const constant另一方面,它将是一个const指针char.因为const它在命名空间范围内默认具有内部链接.

但是,您不应该使用字符串文字对其进行初始化(因为编译器警告指出),因为标准不允许这样做(这种转换在c ++ 11中是非法的,在此之前已被弃用).允许将字符串文字存储在只读内存中,并且不允许您修改它们的运行时.这就是为什么指向带有指向非的指针的字符串文字const char是危险的.