• Advertisement
Sign in to follow this  

[c++] Class Inheritance, static Data Member

This topic is 3582 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

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; }
}

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Ben091986
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 ?
Accessing 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;

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
Quote:
Original post by Ben091986
one solution i found was


... seriously, did you even look at my reply?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement