Dynamical class inheritance

I'm looking for a smart way of doing some sort of dynamical class inheritance.

This can best be explained with the psudo example below. Think of a base class to store information and smaller classes to display that information in different ways.

SetValues() are just a function to set values or whatever with the common variables, etc. in the Base class.

SetDisplay() is a switch to set what of the smaller classes will recieve the call to PaintDisplay().

And PaintDisplay() paints different views of the Base class.

Normal class inheritance says that if I call Red::PaintDisplay() then PaintDisplay() in Red will be called. What I want is to call Base::PaintDisplay() and have one of the smaller classes ( Red, Green, Blue ) catch that call to its own function, depending of what information Base class has - in this case what smaller class gets that function call depends on what Base::SetDisplay() are set to.

So I want the caller of Base::PaintDisplay() to not know about the other small classes but still indirectly call their PaintDisplay() functions depending of what is set through Base::SetDisplay().

Man I feel that this was difficult to explain but I hope some got it and has some ideas of what can be done here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Base
{
    SetValues();
    SetDisplay();
    PaintDisplay();
}
class Red : Base
{
    PaintDisplay();
}
class Green : Base
{
    PaintDisplay();
}
class Blue : Base
{
    PaintDisplay();
}

Try defining a nested class with a pure virtual inside your "base" and set it as private or protected. This will guard the local class from being accessed by users. Then split your R G and B classes off of this base and define your paintdisplay() as virtual in all four of them. Inside your base (the class "base") create a member that is a pointer to the base of the rgb hierarchy. You decide which paintdisplay to call by which object you actually bind to the object.
There may be more elegant solutions, but off the top of my head that's how I'd do it.
Thanx for your quick reply. But could you please give me an example. As I see it you say to have PaintDisplay() as pure virtual in Base which is something I thought anyway. But then you say to have PaintDisplay pure virtual in the RGB classes too. And this is where I'm lost. If all the classes are gonna have PaintDisplay as pure virtual it's not gonna work. It's not even going to compile correctly. Could you please show an example of what you're thinking. Thanx again!
That's called polymorphism.
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
36
struct A{
    virtual void f()=0; //Pure virtual function. Ensures that the derived
                        //classes implement this interface.
};

struct B:A{
    void f(){
        std::cout <<"f() of B"<<std::endl;
    }
};

struct C:A{
    void f(){
        std::cout <<"f() of C"<<std::endl;
    }
};

struct D:A{
    void f(){
        std::cout <<"f() of D"<<std::endl;
    }
};

void f(A *a){
    a->f();
}

int main(){
    B b;
    C c;
    D d;
    f(&b);
    f(&c);
    f(&d);
    return 0;
}
Last edited on
Thanx! I will work on this.
Topic archived. No new replies allowed.