I wonder why it ain't possible to manipulate with static member of base class from derived class.
#include <cstdlib>
#include <iostream>
using namespace std;
class Base{ public: static double BaseValue;
void Set(double a){BaseValue=a;}
void GetBaseValue(){cout<<BaseValue<<endl;}};
class Derived:public Base{
public:
double Count(){
return BaseValue*2;
}
};
int main(int argc, char *argv[])
{
Base Object;
Object.Set(123.3);
Object.GetBaseValue();
Derived Object1;
cout<<Object1.Count()<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
If all elements from base class are derived ( except default constructor and destructor, assignment operator and friends ), static member BaseValue should be derived too.
But I get an error [Linker error] undefined reference to `Base::BaseValue'
when I wish to use BaseValue in some of derived classes.
I tried to create one static member per each class and make them equal to the static member of base class, so it'd look like:
static double DerivedValue=BaseValue;
In hope I will be able to "fake" it, and use equal static member of derived class, as if I was using static member of base class.
I get error: `Base::BaseValue' cannot appear in a constant-expression
The same one happens when I wish to create pointer to the BaseValue.
Can anyone explain me how do you create static member in Base class and then use the same static member within derived classes?
Thanks.