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
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
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 ?
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.
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:
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)
[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 . root.cern.ch/phpBB2/viewforum.php?f=7 is more appropriated templatedTest.cxx (425 Bytes)