If I have a string variable in a header file which will be used across different files and translation units, should I store it in a constexpr const char* or const std::string?
Here is an example:
// config.h
constexpr const char* string_a = "stringA"; // <--should i declare it like this?
const std::string string_b = "stringB"; // <--or like this?
// a.h
#include "config.h"
class A{
public:
A();
};
// a.cpp
#include "a.h"
A::A(){
cout << string_a << endl;
}
// b.h
#include "config.h"
class B{
public:
B();
};
// b.cpp
#include "b.h"
B::B(){
cout << string_b << endl;
}
// main.cpp
#include "a.h"
#include "b.h"
int main(){
A a;
B b;
cout << string_a << endl;
cout << string_b << endl;
return 0;
}
I have read from books that it is recommended to always use string instead of char*, but I have also heard that constexpr is initialized in compile time which is quite good.