0

I'm trying to make some kind of strange polymorphism on C++:

I have two trees of classes:

State <-- Stage <-- Stage_1, Stage_2, Stage_3

Action <-- ActionState <-- ActionState_Clear, ActionState_Begin, ActionState_[...]

I want to define some methods on Stage_1 that get an Action as parameter:

State 
{
    virtual void exec_action(Action *tAct)
    {
        std:cout << "NOT IMPLEMENTED" << endl;
    };
}

Stage_1 
{
    virtual void exec_action(ActionStage_Clear *tAct)
    {
        std::cout << "ACTION STAGE CLEAR" << endl;
    }

    virtual void exec_action(ActionStage_Begin *tAct)
    {
        std::cout << "ACTION STAGE BEGIN" << endl;
    }
}

Action *pAct = new ActionStage_Clear();
State* pSta = new Stage_1();

**pSta->exec_action(pAct);**

I'd like it to show 'ACTION STAGE CLEAR', but it shows 'NOT IMPLEMENTED'. That's - I think - because there isn't any implementation of exec_action(Action *) (although there are some for its son-classes).

EDIT: I'll show you a little part of the diagram: I have Stage(s) and Action(s). I'll have a ptr to Stage_1, 2 .. and a ptr to Action_1, 2 ... I want to call Stage_n->exec_action(Action_n) and that should execute the code of the son: Action_1::exec_action(Action_1). (if it's not implemented it should call Action::exec_action(Action)).

Design The question is

How can I fix it? Is there any other way to make it work?

Thanks, forgive my English.

3
  • This is quite obviously not your real code; please post your actual code. Commented Jul 31, 2011 at 18:26
  • Is this your actual C++ code? I don't get where you define classes. Also, where do you inherit? Commented Jul 31, 2011 at 18:26
  • not real code (obviously!), I post it now Commented Jul 31, 2011 at 18:27

2 Answers 2

2

You want double dispatch here. Virtual functions directly provide only single one. One good example of double dispatch is in Visitor pattern, which could be directly applicable to your task.

Sign up to request clarification or add additional context in comments.

8 Comments

so, I just have to implement the Visitor pattern, that is possible in C++, Isn't it?
@josec, you have to implement double-dispatch, visitor is an example. It depends on the details of your design. It's definitely possible in C++, as anything else.
I have seen the Visitor Patterns but I have a question. If i'd implement that solution, would I have to include all the 'NOT IMPLEMENTED' methods on State? : class State { virtual exec_action(ActionStage_[....] *tAct); .... }
@josec, looks like it, but you don't have to implement Visitor.
So, do you have any idea of how to implement my code without defining all the virtual methods on State? I have tried, and that's the only way I make it work :(
|
1

You can only override in your base classes contravariantly: the type of the argument in the derived class method must be less derived than the one in the base class.

Otherwise (as in your case), you are just writing a different function.

4 Comments

I see... so any idea of how to make it work? I edited the post, to show my intentions
@josec: you really want double dispatch here. Visitor pattern, albeit cumbersome to implement, may be the solution if 1) your inheritance hierarchy is small 2) you expect the number of actions to grow.
I expect the number of actions to grow a lot (there are 3 kinds of Actions --> ActionObject, ActionStage, ActionPhase --> ActionObject_Create, ActionObject_MoveLeft .... and there are 3 kinds of States --> ObjState, Stage, Phase --> ObjState_Idle, ObjState_Jumping ..... I've solved this problem by implementing Visitor pattern, but I ve to declare all the virtual exec_actions on the 'father' class State
@josec: this is indeed one way to go. It is important that you realize what double dispatch is, and that such boilerplate code is necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.