1

I'm trying to implement a template for states machines in C++, but I'm not sure how to handle static object members. Each state machine would be defined by its state variables and its transitions (which are structs). For instance:

// stm.h template <class T> struct transition { ... transition relevant data depending on state variables T ... }; template <class T> class stm { T *stv; // state variables static struct transition<T> *transitions; // I would like to have only one copy of transitions for stm's of type T ... etc ... }; 

Now, suppose I'm trying to implement the stm foo:

// foo_defs.h struct foo_stv { char a; int b; }; // foo_transitions.h // Note that I'm using a different file to keep everything in order #include "stm.h" #include "foo_defs.h" struct transition<struct foo_stv> foo_transitions[] = { { ... trans1 ... }, ..., { ... transN ... } }; // foo_stm.h #include "stm.h" #include "foo_defs.h" #include "foo_transitions.h" class foo_stm { stm<struct foo_stv> stm; struct foo_stv stv; ... other data ... }; 

Ok. So each instance of foo_stm should have a different set of state variables, but all instances should use the same transitions. The question is, how should I define/declare stm<struct foo_stv> in order to use foo_transitions as its class level transitions member?

Also any advice about C++ coding customs is welcome, since I come from the C coding world and just getting started with some C++ mechanics.

EDIT: Just to make clear my question, I guess you should forget about the stm logic and focus on the following: What's the correct way of doing something like...

foo_stm::stm.transitions = foo_transitions; 
10
  • why not a singleton pattern ? The problem will be that you can not initialize the static member except in the cpp, OR if you use recent c++. Commented Sep 1, 2013 at 21:48
  • 1
    "Also any advice about C++ coding customs is welcome, since I come from the C coding world and just getting started with some C++ mechanics" OK: struct keyword is not needed in C++ (Im talking about the sentences like stm<struct foo_stv>. The only difference between class and struct is that the default scope of class members is private, in struct is public. Commented Sep 1, 2013 at 22:11
  • "each instance of foo_stm should have a different set of state variables, but all instances should use the same transitions." How can you have different state variables and the same transitions? Commented Sep 1, 2013 at 23:24
  • @BartoszKP Because the transition struct is composed of call back functions that use as one of its arguments the state vars of the instance stm. Commented Sep 2, 2013 at 2:44
  • @dzada Not sure if I could apply that to my particular problem, but I'll search info about it. Thanks. Commented Sep 2, 2013 at 2:45

1 Answer 1

2

All right, I think that what you want to do is:

template <class T> class stm { T *stv; static transition<T> *transitions; }; template<class T> transition<T>* stm<T>::transitions; //define static template member 

And then:

//define static member for particular template instantiation template<> transition<foo_stv>* stm<foo_stv>::transitions = foo_transitions; class foo_stm { stm<struct foo_stv> stm; struct foo_stv stv; ... other data ... }; 
Sign up to request clarification or add additional context in comments.

3 Comments

Interesting, I'll try this when I get home. I'm trying to desing in the context of OOP, but not used to it. Where am I failing?
I'll try to give you a more detailed answer soon, sorry for the delay.
All right, I've analysed your solution more thoroughly and I think it's quite OK, in the context template programming. What I saw wrong the last time, was the fact that you're abusing a bit the "struct" keyword (C-style), and it blurred my vision a bit :-) Assuming that your transition structure contains a pair of states, and optionally some additional transition data all seems to be OK. Sorry for the misleading comment in my previous answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.