Idea ? : inheritance semi-shared static attribute

Hi,
I had troubles doing something simple and I wanted to figure out if the idea I come-up with is something which might be submitted for C++ improvements or not.

I wanted to create an abstract class A:
1
2
3
4
5
6
7
class A
{
//methods
//attributes
protected:
static const std::string id;//not specified
};

and I wanted to create some class inheriting it which will have to specify their own id in the cpp.
1
2
3
4
5
6
7
8
9
10
11
12
class B:public A
{};
class C:public A
{};
//in CPP
const std::string B::id("B_name");
const std::string C::id("C_name");
/*ie what I want is
C::id is different from B::id,
C:: id is accessible from any C objects,
B::id accessible from any B object
*/


I know that current static keyword don't work like that because they can't have something different per inherited class (any change of id will impact all inheriting classes) and that you must give value to A::id.

So do you think a new keyword (static_class) might be useful to do what I want ?
Or do you think it is better to use templates, as from C++11 you can templatize with a const_expr string ?

Thanks for figuring it out
Last edited on
Or just use overloaded accessors which do not break encapsulation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

class A
{
public:
    virtual const std::string& get_id() const = 0;
};

class B:public A
{
public:
    virtual const std::string& get_id() const override
    {
        static const std::string id("B_name");
        return id;
    }
};
class C:public A
{
public:
    virtual const std::string& get_id() const override
    {
        static const std::string id("C_name");
        return id;
    }
};

int main()
{
    A* arr[5] = {new B, new C, new C, new B, new C};
    for(const A* x: arr) {
        std::cout << x->get_id() << '\n';
        delete x;
    }
}
B_name
C_name
C_name
B_name
C_name


Also if I understood your example correctly, you cannot have static members to behave poliformly, because it makes nos sense for them. Static members invokes on class, not instance, where you can get information about real class.
Last edited on
Thanks that's what I was looking for !
Is there somewhere some deep explanation on the : override + static const variable ?
Is the variable recreated for each call ? or once for all ? (here id is string but maybe it can be a class more complex later, a class you don't want to be recreated on each call)
static means that there is only one instance of type shared between all function calls. It will be created when first required.

I could make it something like:
1
2
3
4
std::string get_id()
{
    return "C_name";
}
But it would include creation of variable each call, so for sake of efficiency I made it static.
I added const to variable itself and to return type because I do not want for outside code to change this string.
I added const as qualifier of member function to make it avaliable on constant class instances.

Override means that this function should override some virtual function from parent class. It will generate error if it would not. It saves you from accdental errors when derived class does not actually override parent class function.

For example if I would write virtual const std::string& get_id(), I will not override parent class function because of missing const-qualifier. However if I add overide to the end, compiler will threat this as error, allowing me to see and fix my mistake.
Thanks, I didn't know for static variable.
Topic archived. No new replies allowed.