简体   繁体   English

C++ 中的全局常量字符指针数组

[英]Global const char pointer array in C++

I have defined a const char pointer array in header file.我在头文件中定义了一个 const char 指针数组。 Array contains multiple strings.数组包含多个字符串。 Now I have to find a string at particular index.现在我必须在特定索引处找到一个字符串。 I don't know how should I access this array from another files.我不知道我应该如何从另一个文件访问这个数组。 Please see my code:请看我的代码:

common.h通用文件

extern const char *lookup_str[] = {"test Str0", "test Str1", "test Str2", "test Str3"};

file1.cpp文件1.cpp

int ret = 3;
std::string r = lookup_str[ret];

Can I use this way in all my C files?我可以在我所有的 C 文件中使用这种方式吗? Let me know if you can see any alternative approach.如果您能看到任何替代方法,请告诉我。 Any suggestion/help is much appreciated.非常感谢任何建议/帮助。 Thank you in advance !先感谢您 !

Declare the variable without initializing it in the .h file:在 .h 文件中声明变量而不初始化它:

extern const char *lookup_str[];

Initialize it in exactly one source file.在一个源文件中初始化它。

const char *lookup_str[] = {"test Str0", "test Str1", "test Str2", "test Str3"};

To use it in a .cpp file, use:要在 .cpp 文件中使用它,请使用:

#include "common.h"

...

int ret = 3;
std::string r = lookup_str[ret];

A better alternative, IMO, would be to provide a purely functional interface to the data. IMO 更好的替代方案是为数据提供纯功能接口。

The .h file: .h 文件:

std::string const& lookup_str(size_t index);

The .cpp file: .cpp 文件:

std::string const& lookup_str(size_t index)
{
   static std::vector<std::string> str = {"test Str0", "test Str1", "test Str2", "test Str3"};
   return str.at(index);  // This will throw exception if index is not
                          // within bounds.
}

and then use it as:然后将其用作:

#include "common.h"

...

size_t ret = 3;
std::string r = lookup_str(ret);

Update更新

A C++98 implementation of lookup_str() would be: lookup_str() C++98 实现将是:

std::string const& lookup_str(size_t index)
{
   static std::vector<std::string> str;
   if ( str.empty() )
   {
      str.push_back("test Str0");
      str.push_back("test Str1");
      str.push_back("test Str2");
      str.push_back("test Str3");
   }
   return str.at(index);
}

You should avoid having variable definitions in header files.您应该避免在头文件中包含变量定义。 Instead, you can have a declaration , which simply informs the compiler of a symbol, which can be defined elsewhere, and compiled separately.相反,您可以有一个声明,它只是通知编译器一个符号,该符号可以在别处定义,并单独编译。

So, your header should look like this:所以,你的标题应该是这样的:

extern const char *lookup_str[]; // defined elsewhere

And you should add this to a source (.c) file (doesn't matter which one, as long as it's linked into the final executable):并且您应该将其添加到源 (.c) 文件中(哪个无关紧要,只要它链接到最终的可执行文件中即可):

const char *lookup_str[] = { ... };

Then to use it, simply include the aforementioned header, and use it like you would any array.然后使用它,只需包含上述标题,并像使用任何数组一样使用它。

Since you are doing this in c++ , dont do it with c-strings , do it the c++ way:由于您是在c++中执行此操作,因此不要使用c-strings执行此c-strings ,请使用 c++ 方式执行此操作:

common.cpp通用文件

std::array<std::string, 4> lookup_str = {"test Str0", "test Str1", "test Str2", "test Str3"};

common.h通用文件

extern std::array<std::string, 4> lookup_str;

Then you can use it in the same way:然后你可以用同样的方式使用它:

int ret = 3;
std::string r = lookup_str[ret];

Or in awesome c++ ways:或者以很棒的 C++ 方式:

auto stringInArr = std::find(lookup_str.begin(), lookup_str.end(), "test Str3");

Here is a live example:这是一个活生生的例子:

http://ideone.com/UFfIyV http://ideone.com/UFfIyV

A cleaner design IMO would be to declare the extern and definition in a header file separated by macro definition更简洁的设计 IMO 是在由宏定义分隔的头文件中声明 extern 和定义

strdef.h strdef.h

#ifdef STRING_INSTANCE_DEFINE
const char *lookup_str[] = {"test Str0", "test Str1", "test Str2", "test Str3"};
#else
extern const char *lookup_str[];
#endif

strdef.c strdef.c

#define STRING_INSTANCE_DEFINE
#include <strdef.h>

All other files所有其他文件

#include <strdef.h>

Remeber to define the macro STRING_INSTANCE_DEFINE only in 1 .c file.记住只在 1 个.c文件中定义宏STRING_INSTANCE_DEFINE All other files using the array could simply include the header file and it would be available to them as extern.使用该数组的所有其他文件可以简单地包含头文件,并且它们可以作为 extern 使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM