sud sud - 2 months ago 6
C++ Question

Is it appropriate to set a value to a "const char *" in the header file

I have seen people using 2 methods to declare and define

char *
.

Medhod 1: The header file has the below

extern const char* COUNTRY_NAME_USA = "USA";


Medhod 2:

The header file has the below declaration:

extern const char* COUNTRY_NAME_USA;


The cpp file has the below definition:

extern const char* COUNTRY_NAME_USA = "USA";



  1. Is method 1 wrong in some way ?

  2. What is the difference between the two ?

  3. I understand the difference between "
    const char * const var
    " , and "
    const char * var
    ". If in the above methods if a "
    const char * const var
    " is declared and defined in the header as in method 1 will it make sense ?


AnT AnT
Answer

The first method is indeed wrong, since it makes a definition of an object COUNTRY_NAME_USA with external linkage in the header file. Once that header file gets included into more than one translation unit, the One Definition Rule (ODR) gets violated. The code will fail to compile (more precisely, it will fail to link).

The second method is the correct one. The keyword extern is optional in the definition though, i.e. in the cpp file you can just do

const char* COUNTRY_NAME_USA = "USA"

assuming the declaration from the header file precedes this definition in this translation unit.

Also, I'd guess that since the object name is capitalized, it is probably intended to be a constant. It so, then it should be declared/defined as const char* const COUNTRY_NAME_USA (note the extra const).

Finally, taking that last detail into account, you can just define your constant as

const char* const COUNTRY_NAME_USA = "USA"; // no `extern`!

in the header file. Since it is a constant now, it has internal linkage by default, meaning that there is no ODR violation even if the header file is included into several translation units. In this case you get a separate COUNTRY_NAME_USA lvalue in each translation unit (while in extern method you get one for the entire program). Only you know what you need in your case .

Comments