简体   繁体   中英

Global const char pointer array in C++

I have defined a const char pointer array in header file. 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

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

Can I use this way in all my C files? 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:

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:

#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.

The .h file:

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

The .cpp file:

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:

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):

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:

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:

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

Here is a live example:

http://ideone.com/UFfIyV

A cleaner design IMO would be to declare the extern and definition in a header file separated by macro definition

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

#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. All other files using the array could simply include the header file and it would be available to them as extern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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