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.

Recommended Answers

All 3 Replies

You have to move the static variable static double BaseValue into a .h header file. You cant have it in the .cpp file.

Then it will compile and run error free.


Put this in the .h file:

static double BaseValue;

Put this in the .cpp file:

#include <cstdlib>
#include <iostream>
#include "staticmbr.h"

using namespace std;
class Base
{ 

	public: 

        Base()
		{
		}
		

		void Set(double a)
		{
			BaseValue=a;
		} 
		void GetBaseValue()
		{
			cout<<BaseValue<<endl;
		}
 };
 
class Derived:public Base
{ 
		  public:
		  Derived()
		  {
		  }

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

output:

123.3
246.6

You have to move the static variable static double BaseValue into a .h header file.

This is nice solution, but what if I have to ensure my BaseValue can't be called from main() or any other non-class and non-derived-class location?

I have found way to manipulate with static member through derived class.

#include <cstdlib>
#include <iostream>

using namespace std;

class Base{
      protected:
                static int Value;
      public:
              Base(){Value=0;};
              Base(int a){Value=a;};
              void Set(int a){Value=a;}
              void Print(){cout<<Value<<endl;}
      };
int Base::Value=0;      //It's all about this line
class Derived:public Base{
      public:
      Derived(int a){Value=a;}
      
};

int main(int argc, char *argv[])
{
    Base Object(5);
    Object.Print();
    Derived Obj(3);
    Obj.Print();
    Object.Print();
  
    system("PAUSE");
    return EXIT_SUCCESS;
}

As you can see, if I don't fill Value with any value ( Value- variable, value - {1,2,3..} ), I can't use it.

I guess this is needed because I have declared the static member without any value, but it is same for many classes, it'd occur errors.
But if someone smarter could explain me, I'd really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.