Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?
like image 427
sud Avatar asked May 21 '10 04:05

sud


People also ask

What does const char * means?

const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant char.

Can I assign a string in a const char *?

You absolutely can assign const char* to std::string , it will get copied though.

Can a char * be passed as const * argument?

In general, you can pass a char * into something that expects a const char * without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can't pass a const char * into something expecting a char * (without an explicit cast) because that's ...

What is the difference between const char * and string?

string is an object meant to hold textual data (a string), and char* is a pointer to a block of memory that is meant to hold textual data (a string). A string "knows" its length, but a char* is just a pointer (to an array of characters) -- it has no length information.


2 Answers

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

like image 190
AnT Avatar answered Oct 18 '22 21:10

AnT


What's the point?

If you want to lookup strings (that could be localized), this would be best:

namespace CountryNames {
    const char* const US = "USA";
};

Since the pointer is const, it now has internal linkage and won't cause multiple definitions. Most linkers will also combine redundant constants, so you won't waste space in the executable.

If you want to compare strings by pointer equality though, the above isn't portable because the pointers will only be equal if the linker performs the constant-folding optimization. In that case declaring an extern pointer in the header file is the way to go (and it again should be const if you don't intend to retarget it).

like image 28
Ben Voigt Avatar answered Oct 18 '22 22:10

Ben Voigt