In C++ I wanted to define a constant that I can use in another function, A short answer on how to do this will be fine..
Lets say at the beginning of my code I want to define this constant:
//After #includes
bool OS = 1; //1 = linux
if (OS) {
const ??? = "clear";
} else {
const ??? = "cls";
}
I don’t know what type to use to define the “clear” string… I’m so confused.
Later on I want to use it within a function:
int foo() {
system(::cls); //:: for global
return 0;
}
How would I define the string up top, and use the string down below? I heard char only had one character and things… I’m not sure how to use , since it says it’s converting string into const char or something.
char*isn’t quite achar.char*is basically a string (it’s what strings were before C++ came along).For illustration:
char[]degrades tochar*, so you’ll often see functions take achar*.To convert
std::stringtoconst char*, you can simply call:In this case, it’s common to use the preprocessor to define your OS. This way you can use the compiler to do the platform specific stuff:
One thing you may want to consider is making it a function. This avoids nasty dependencies of global construction order.
What it looks like you’re trying to do is this: