Shared header.
I can do this:
const bool kActivatePlayground=false;
Works fine when included among multiple files.
I cannot do this:
const char * kActivePlayground = "kiddiePool";
Results in error: duplicate symbols.
But this works:
static const char * kActivePlayground = "kiddiePool";
Why is the static
needed for the const char *
but not for the const bool
? Additionally, I thought static
is not necessary since const
is always static
implicity?
In C++, const
variables by default have static linkage, while non-const
variables have external linkage.
The reason for the multiple definitions error is that
const char * kActivePlayground = "kiddiePool";
creates a variable with external linkage.
Hey wait, didn't I just say that const
variables default to static linkage? Yes I did. But kActivePlayground
is not const
. It is a non-const
pointer to const char
.
This will work as you expect:
const char * const kActivePlayground = "kiddiePool";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With