1
void ChangeState(int newState)
{
    nextState = newState;

    //Change the state
    switch( nextState ) ///the change still will have a case for everything
    {
    case STATE_INTRO:
    {
        vec.pop_back();
        state ptr(new CIntroState);
        vec.push_back(ptr);
        break;
    }
    case STATE_MENU:
    {
        vec.pop_back();
        state ptr(new CMainMState);
        vec.push_back(ptr);
        break;
    }
    }
}

I have this function that allows me to change my current state; however, I thought about it and this is going to be a huge switch statement by the time I am finished. Currently, it already has about 10 states in it, this is just some sample code. I am trying to do something a little different, but I am not sure how to go about it.

void ChangeState(something)
{
    vec.pop_back();
    state ptr(new something);
    vec.push_back(ptr)
}

If I could bring it in this way, I could completely avoid the use of the switch statement and get the same end result. Does anyone know how to go about this? Any help would be greatly appreciated.

Chivos
  • 83
  • 1
  • 6
  • 1
    Looks like you want templates. – GManNickG Mar 11 '13 at 23:35
  • What makes each state different? Perhaps you just need a `State` class with data members that describe these differences rather than a separate class for each state. – Code-Apprentice Mar 11 '13 at 23:37
  • 1
    What is know about something. If you give each state `something` its own type. could you know the type at compile-time. Or is it only possible to know it in run-time? – AxelOmega Mar 11 '13 at 23:37
  • each states runs three functions, updatelogic, handling events, and drawing. I know each state and runtime/compile time. When to change states is all programmed in. – Chivos Mar 11 '13 at 23:43
  • Incidentally, why the pushing and popping? Why not `vec.back() = new something;`? – Konrad Rudolph Mar 11 '13 at 23:48
  • Well, I was doing the pop back because the new something that is held in vec.back() is a smart pointer and I thought that I would have to popback in order to ensure the d-constructor was called; however, I never tested it with just switching what vec.back() was.. So I will try that out ;) – Chivos Mar 11 '13 at 23:57

2 Answers2

1

It looks like you need a combination of templates and polymorphism. With templates, you don't need to enumerate all the different types that you may pass into a function, as this will be done for you during compilation. To store all of these different types in the same vector, you'll need to have them all sub class some super-type which then allows you to store them side by side in a vector of super-type.

ryanbwork
  • 2,123
  • 12
  • 12
  • I have them all taken from an abstract class so they all have the same type. I tried to accomplish this with templates, but with little success... ;) – Chivos Mar 11 '13 at 23:45
  • Can you post the code you wrote that used a template function + the errors you were getting? – ryanbwork Mar 11 '13 at 23:46
  • I think I got rid of it because of how frustrated I was at the time lol. I will have to rewrite it and try to mess with it again. For some reason, templates is something that I understand the concept behind them, but have not figured it out yet heh. – Chivos Mar 11 '13 at 23:59
1

You need to link compile-time polymorphism (i.e., templates) and run-time polymorphism, as ryanbwork suggests. The challenge here is to try to avoid transforming your current form of repetitive code (the big switch) into a similarly verbose but more declarative form of repetitive code.

As another popular Stack Overflow question reveals, clone methods are usually the way one creates new instances of dynamically determined types at run-time. Cloning is really a specialization of the more general factory pattern, and you could create a set of factories for your types whose definition would be only a little more verbose than your current enumeration presumably is:

template <class T>
class factory {
public:
   T* operator()() {return new T;}
};

factory<CIntroState> IntroState;
factory<CMainState>  MainState;
// etc...

template <class Fac>
void ChangeState(Fac newStateFactory)
{
   vec.pop_back();
   vec.push_back(newStateFactory());
}
Community
  • 1
  • 1
Jollymorphic
  • 3,510
  • 16
  • 16
  • aww, thank you. I think this pretty much covered my question! Thanks for the link btw! – Chivos Mar 12 '13 at 00:15
  • Happy to oblige, @user1956980. I notice from your other questions, though, that you don't seem to be in the habit of accepting answers. Perhaps it's a feature of StackOverflow with which you are unfamiliar. In any case, it's CUSTOM MADE for just this situation. See instructions here: http://stackoverflow.com/about – Jollymorphic Mar 12 '13 at 03:15
  • You are correct. That is a feature I was unfamiliar with! I have accepted your answer! thank you again. – Chivos Mar 12 '13 at 17:14