static member variables and inheritance

I have following problem, normally when you have the same static in base and derived, you'd end up with 2 different statics:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A
{
public:
	static const char* num;
};

const char* A::num = "-1";

class B : public A
{
public:
	static const char* const num;
};

const char* const B::num = "1337";

const char* a = A::num //"-1"
const char* b = B::num //"1337" 


But somehow in another part somewhere in my program
B::num is "-1"
what could be the cause for this?
Maybe it cannot see the B::num, and therefore takes the base A::num?


ps: it definitely must be the case that it cannot see the B::num beacuse creating a seperate B::num2 causes an error



Last edited on
ok problem found:

this part of the code didn't include the necessry .h file, and since all was done through the static member inheritance it didn't cause an error and just took the base value.

never had to deal with such a case, but it interesting.

Is this even a good design to create statics with the same name in the base and derived?
Seems like it's likely to oversee errors like it happened to me

this part of the code didn't include the necessry .h file, and since all was done through the static member inheritance it didn't cause an error and just took the base value.


How did you do "B::num", when the header that defined B wasn't included? :o

Is this even a good design to create statics with the same name in the base and derived?


Absolutely not.
> Is this even a good design to create statics with the same name in the base and derived?

For an example of where this is useful, see std::locale::id
http://en.cppreference.com/w/cpp/locale/locale/id
hmm true, the main problem ist just it can cause lots of issues when someone creates a derived and forgets to define the static, the way it happened to me.

is there actually a way to generate compile time errors for such cases?
https://stackoverflow.com/questions/10502114/how-to-enforce-a-static-member-on-derived-classes

kinda answered my question, if anyone else has an idea, let me know
Topic archived. No new replies allowed.