在哪里定义一个const字符串? [英] Where to define a const string?
问题描述
我正在寻找一种优雅的方式来声明一个字符串const,它可以在许多文件中显示为
。
如果我这样做:< br $>
//☆hdr.h
const char * sFoo =" foo";
//file.cpp
#include< hdr.h>
strcpy(string,sFoo);
//anotherfile.cpp
#include< hdr.h>
strcpy(string,sFoo);
链接器抱怨sFoo是多重定义的。
我不想使用#define,因为它打破了类型安全。我不想拥有
多份''
const char * sFoo =" foo";''乱丢我的代码。
最简洁,最易于维护的方法是什么?
RDeW
I am looking for a graceful way to declare a string const that is to be
visible across many files.
If I do this:
//----hdr.h
const char * sFoo = "foo";
//file.cpp
#include <hdr.h>
strcpy(string, sFoo);
//anotherfile.cpp
#include <hdr.h>
strcpy(string, sFoo);
The linker complains that sFoo is multiply defined.
I don''t want to use a #define as it breaks type safety. I don''t want to have
multiple copies of ''
const char * sFoo = "foo";'' littering my code.
What is the most compact and maintainable way to do this?
RDeW
推荐答案
Riley DeWiley写道:
Riley DeWiley wrote:
我正在寻找一种优雅的方式来声明在许多文件中可见的字符串const。
[剪辑示例]
链接器抱怨sFoo是多重定义的。
我不想使用#定义它打破了类型安全。我不想要多份''
const char * sFoo =" foo";''乱丢我的代码。
什么是最紧凑的和可维护的方法吗?
RDeW
I am looking for a graceful way to declare a string const that is to be
visible across many files.
If I do this:
[snip example]
The linker complains that sFoo is multiply defined.
I don''t want to use a #define as it breaks type safety. I don''t want to
have multiple copies of ''
const char * sFoo = "foo";'' littering my code.
What is the most compact and maintainable way to do this?
RDeW
头文件:
extern const char sFoo [];
在(一个)源文件中(完全没关系):
const char sFoo [] =" foo" ;;
-
?? z。?? ii(i((?? n。?? m。?? z。?? i.nz(?? q.mqi))((?? n.??z. ?? in(nzi)i)(?? z.??ii(((????????
(nzi)i)(?? z.??ii(iz)))zi)))(,??n.??z.??in(nzi)i)(?? z。?? ii(iz)))zi))
Header file:
extern const char sFoo[];
In (one) source file (exactly which doesn''t matter):
const char sFoo[] = "foo";
--
??z.??i.i(i((??n.??m.??z.??i.nz(??q.mqi))((??n.??z .??i.n(nzi)i)(??z.??i.i(((??n.??z.??i.n
(nzi)i)(??z.??i.i(iz)))zi)))((??n.??z.??i.n(nzi)i) (??z.??i.i(iz)))zi))
Riley DeWiley写道:
Riley DeWiley wrote:
我正在寻找一种优雅的方式来声明一个字符串const,即在许多文件中都可以看到。
如果我这样做:
//☆hdr.h
const char * sFoo =" foo";
//file.cpp
#include< hdr.h>
strcpy(string,sFo o);
//anotherfile.cpp
#include< hdr.h>
strcpy(string,sFoo);
链接器抱怨sFoo是多重定义的。
我不想使用#define,因为它打破了类型安全。我不想要多份'
const char * sFoo =" foo" ;;''乱丢我的代码。
什么是最紧凑的和维护方法吗?
I am looking for a graceful way to declare a string const that is to be
visible across many files.
If I do this:
//----hdr.h
const char * sFoo = "foo";
//file.cpp
#include <hdr.h>
strcpy(string, sFoo);
//anotherfile.cpp
#include <hdr.h>
strcpy(string, sFoo);
The linker complains that sFoo is multiply defined.
I don''t want to use a #define as it breaks type safety. I don''t want to have
multiple copies of ''
const char * sFoo = "foo";'' littering my code.
What is the most compact and maintainable way to do this?
如果你在标题中声明它
const char * const sFoo =" foo" ;;
创建多个副本。为了避免这种情况,你可以在标题和_one_of_the_C ++ _ source_files_做
extern const char sFoo [];
>
extern const char sFoo [] =" foo";
链接器很高兴您将拥有
程序中的字符串。
V
If you declare it in the header
const char * const sFoo = "foo";
which creates multiple copies. To avoid that you could do
extern const char sFoo[];
in the header and in _one_of_the_C++_source_files_ do
extern const char sFoo[] = "foo";
The linker will be happy and you will have the only definition of the
string in the program.
V
Riley DeWiley写道:
Riley DeWiley wrote:
我不想使用#define,因为它打破了类型安全。
不,它没有。
#define sFoo" foo"
每个使用标识符sFoo的地方你都会得到一个字符串文字。然而,它的
类型是const char的数组,而不是指向const char的指针,
这是你的代码使用的。
我不喜欢我想要多份'
const char * sFoo =" foo";''乱丢我的代码。
什么是最紧凑和可维护的这样做的方法?
I don''t want to use a #define as it breaks type safety.
No, it doesn''t.
#define sFoo "foo"
Every place you use the identifier sFoo you''ll get a string literal. Its
type, however, is array of const char rather than pointer to const char,
which is what your code uses.
I don''t want to have
multiple copies of ''
const char * sFoo = "foo";'' littering my code.
What is the most compact and maintainable way to do this?
在标题中:
const char * sFoo;
在一个实现文件中:
const char * sFoo =" foo" ;;
其他消息的建议,使用const char sFoo [],另外
有效,但就像宏一样,它使sFoo与你要求的
不同。
- -
Pete Becker
Dinkumware,Ltd。( http://www.dinkumware.com )
这篇关于在哪里定义一个const字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!