Overload static data memeber?

dear Object-Oriented experts & gurus

I have a base class, from which about 10 child classes are derived.
I need to add (preferedly in a smart way) static variable to every of these 10 child classes.
[better to say a member sharing same value for all objects of a class]

naively tried to do this

class A
{
protected:
static int a;
}

A::a=0;

class B::public A
{
protected:
static int a;
}
B::a=1;

funny, that the code compiled OK.
but
cout <<“A:” << A::a << endl;
cout <<“B:” << B::a << endl;
gives
A:1
B:1

pls advise

Can you upload the code you really compiled ?

sorry. here it is:

#include <iostream>
using namespace std;
class A
{
protected: 
double a;
public:
 void set(double i){a=i;} 
double* get(){return &a;}
};

//float* A::a=(float*) 1;

class B:public A
{
protected:
  static double a;
public:
};

double B::a= (double) 2;

A a_arr[10];
B b_arr[10];

int main ()
{  
for(int i=0;i<10;++i) 
   {
      a_arr.set((double) i);      
      b_arr.set((double) i);      
      printf("%d       A::a = %p  %2.0f     B::a = %p %2.0f \n",        
      i, 
      (a_arr.get()), 
      *(a_arr.get()), 
      (b_arr.get()), 
      *(b_arr.get()));    
   } 
 return 1;
}

my intention is simple:

class A is a generic parent track class, to which i want to add few data members and (!) methods operating with these members

class B is a child class (specific tracking package):
since data members in class B are common for all objects of class B it makes sence to make them static (or globals) in order to set them/store them and read them from the same memory.

the problem i faced is that calling method B::get(a), gets in fact somehow A::a

here i am confused

May, I repeat my previous question?
Can you upload the code you did compile? The log of your compilation session would spare us a lot of talking time.
At least, my compiler said,

test.cxx: In function `int main()': test.cxx:30: request for member `set' in `a_arr', which is of non-aggregate type `A[10]' test.cxx:31: request for member `set' in `b_arr', which is of non-aggregate type `B[10]' test.cxx:34: request for member `get' in `a_arr', which is of non-aggregate type `A[10]' test.cxx:35: request for member `get' in `a_arr', which is of non-aggregate type `A[10]' test.cxx:36: request for member `get' in `b_arr', which is of non-aggregate type `B[10]' test.cxx:37: request for member `get' in `b_arr', which is of non-aggregate type `B[10]'
Anyway, the problem you are asking about now is not what you had asked originally

Please, first make your code compiled. Address and fix all compiler error and warning messages. If you are confused with the compiler messages upload those messages (i.e. upload the compilation session log, see the first clause above).

It is not clear for me yet what you want to achieve calling TWO different data-members of your class B with one and the same name, namely “a”.

it looks like the forum syntax checker eats up the “i in brackets”.
apparently this entity is reserved for “italic” letters.

[quote=“fine”]
It is not clear for me yet what you want to achieve calling TWO different data-members of your class B with one and the same name, namely “a”.[/quote]

here I lost you. please explain.
what do u call two different data members?
“double a” and “static double a”?
isn’t static declaration allocaiting “a” in B namespace ?


#include <iostream>
using namespace std;
class A{
protected:
// static double a;
  double a;
public: 
void set(const double i){this->a=i;} 
double* get(){return &(this->a);}
};

class B:public A
{
protected:  
static double a;
public:
};

class C:public A
{
protected:  
static double a;
public:
};

//double A::a= (double) 1;
double B::a= (double) 2;
double C::a= (double) 3;

B b_arr[5];
C c_arr[5];

int main ()
{
  for(int k=0;k<5;++k)    
{      
         b_arr[k].set((double) 4);
//      c_arr[k].set((double) 5);      
         printf("%d       C::a = %p  %2.0f     B::a = %p %2.0f \n",         
          i,(c_arr[k].get()), *(c_arr[k].get()),        
           (b_arr[k].get()), *(b_arr[k].get())            
                );    
 }  
return 1;
}

0       C::a = 0x8049ac0   0     B::a = 0x8049a80  4
1       C::a = 0x8049ac8   0     B::a = 0x8049a88  4
2       C::a = 0x8049ad0   0     B::a = 0x8049a90  4
3       C::a = 0x8049ad8   0     B::a = 0x8049a98  4
4       C::a = 0x8049ae0   0     B::a = 0x8049aa0  4


I would like to get this:

0 C::a = 0x8049ac0 3 B::a = 0x8049a80 4
1 C::a = 0x8049ac0 3 B::a = 0x8049a80 4
2 C::a = 0x8049ac0 3 B::a = 0x8049a80 4
3 C::a = 0x8049ac0 3 B::a = 0x8049a80 4
4 C::a = 0x8049ac0 3 B::a = 0x8049a80 4

Please, check the code attached to see whether it does what you wanted.

/tmp/fine/> g++ test.cxx /tmp/fine/> a.out 0 B::a = 22 C::a = 33 1 B::a = 22 C::a = 33 2 B::a = 22 C::a = 33 3 B::a = 22 C::a = 33 4 B::a = 22 C::a = 33 5 B::a = 22 C::a = 33 6 B::a = 22 C::a = 33 7 B::a = 22 C::a = 33 8 B::a = 22 C::a = 33 9 B::a = 22 C::a = 33
test.cxx (697 Bytes)

I have slightly changed my example to be more close to your design

0 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 1 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 2 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 3 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 4 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 5 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 6 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 7 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 8 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000 9 B::a = 22 C::a = 33 address 0x8049010, value 22.000000 address 0x8049018, value 33.000000
test.cxx (1.02 KB)

Hi, Igor.

I guess, you understand difference between static and non-static data member?

In your base class A you have:

non-static data member a. (*)
non-static member functions set/get.

In this functions you are accessing non-static data member a(*).
It doesn’t matter, that in you derived classes you have static data members with the same name ‘a’. Function in base class A refers to non-static data member a, defined in class A. Period. :slight_smile:

And I guess you know, that static data member (say, MyClass::a) is a single object, and exists independently from instances of class MyClass, so to set it - you do not need any instances of class MyClass.

You can set it like this:

MyClass::a = someValue;
//or, for example:
class MyClass
{
public:
   static void setA(int val){a = val;}
   static int getA(){return a;}
private:
   static int a;
};

int MyClass::a = 0;

int main()
{
   MyClass::setA(10);
   std::cout<<MyClass::getA();
}

And your original question:
if you have several classes with static data members with the same name,
you can simply directly qualify their names:

A::a = 10;
B::a = 10;
C::a = 10;

yes, thanks,
it does exactly what I want.

there is always some “but”, here it is:
I am looking for technique that would allow me to define in one place member functions and in another place data members.

your solution does perfectly what I need, but with it I get the same what I wanted to escape.
namely: defining both data members and member functions for each class individually.

which add repetitions of the same code in the 7 different class (although related)

thanks

[quote=“rubinsky”]yes, thanks,
I am looking for technique that would allow me to define in one place member functions and in another place data members.[/quote] This goes against of the “energy conservation” law. One can define the so-called “abstract interface”. However, one must provide the concrete code to do the concrete job behind that “abstract” interface.

[quote=“rubinsky”]
which add repetitions of the same code in the 7 different class (although related)[/quote] It is not the “same”, otherwise you would not have separated it or . . .
The entire discussion suggests you have some problem with your original design (data-model) to be revised. Since I am not aware about your “model” it is hard to make any advice though … see the attachment.
and . . . let’s change the forum. It is not “ROOT Support” issue anyway :unamused: . root.cern.ch/phpBB2/viewforum.php?f=7 is more appropriated :bulb:
templatedTest.cxx (425 Bytes)