user612308 user612308 - 4 years ago 208
C++ Question

const string vs. #define

i need to share some strings in my c++ program. should i use #define or const string? thanks


#define str1 "str1"
#define str2 "str2"



extern const string str1;
extern const string str2;


const string str1 = "str1";
const string str2 = "str2";

Answer Source

Prefer the second option. If you use the first option (preprocessor), you are limiting your flexibility with the object.

Consider the following... You won't be able to compare strings this way:

if (str1 == "some string")
    // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download