[c++] Class Inheritance, static Data Member
Hi folks,
I have a class with a static integer.
class Foo
{
static const int key;
}
and every derived class must have its own key,
so a class
class Bar: public Foo
{
//what so ever
static const int key = 1;
}
class Bar2: public Foo
{
//what so ever
static const int key = 2;
}
must have a its own key. regardless of instance.
The problem is that if i am dealing with base class pointers, i cannot access that static key.
Foo* a::key; //will give linking error, cause key is not initialised.
if i initialize that key for Foo, then what ever my class Bar defines, i will get the value of foo key and not bar key.
You've created three different variables, named Foo::key, Bar::key and Bar2::key. Since there are no virtual static variables in C++, any attempt to reference one of these will be statically bound, meaning it will be independent of the dynamic type of whatever object you're manipulating.
Use a virtual function instead.
Use a virtual function instead.
class Foo{ virtual int key() const = 0;};class Bar: public Foo{ virtual int key() const { return 1; }},class Bar2: public Foo{ virtual int key() const { return 2; }}
You can't do that. A "static" variable means "There will only be one of these in the entire lifetime of the application". That means you can't change its value from derived classes, since then there would be more than one (One for each class type).
You could either make the variable non-static (In which case every instance of the class will have one), or you could use a virtual function that returns the key instead (That'll mean no per-instance overhead, only per-classtype).
You could either make the variable non-static (In which case every instance of the class will have one), or you could use a virtual function that returns the key instead (That'll mean no per-instance overhead, only per-classtype).
hmm i cant make static functions virtual ...
so i cannot give every type of a derived class a fixed nr. that i can get without having pointer of that class ?
so i cannot give every type of a derived class a fixed nr. that i can get without having pointer of that class ?
Quote:Original post by Ben091986Accessing a static variable requires that you know the type of the class at compile time. You'd have to have a seperate static variable for each class type, and access it like int blah = Bar2::key;
hmm i cant make static functions virtual ...
so i cannot give every type of a derived class a fixed nr. that i can get without having pointer of that class ?
one solution i found was
class foo
{
virtual getKey() = 0;
}
class bar
{
int getKey (){return key;}//important key unique for every state!!
static const int key = 1;
}
so i can have that static key with
bar::key or a base pointer
foo* a = a->getKey();
class foo
{
virtual getKey() = 0;
}
class bar
{
int getKey (){return key;}//important key unique for every state!!
static const int key = 1;
}
so i can have that static key with
bar::key or a base pointer
foo* a = a->getKey();
Quote:Original post by Ben091986
one solution i found was
... seriously, did you even look at my reply?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement