1

In C#:

public sealed class StateMachine<TState, TTrigger>

I'd like to write a C++ equivalent.

Xeo
  • 129,499
  • 52
  • 291
  • 397
CodingHero
  • 2,865
  • 6
  • 29
  • 42

3 Answers3

3

Like this:

template <typename TState, typename TTrigger>
class StateMachine
{
    //...
};
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
1

This site has a pretty good explanation of how to make template classes. Example:

// function template
#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

Use this in combination with this previous Stack Overflow question to achieve the sealed aspect of the class.

Community
  • 1
  • 1
vette982
  • 4,782
  • 8
  • 34
  • 41
0

You could use what Stuart and Xeo proposed, and if you want to make the class sealed here is link that explains how to do it.

EDITED: this is even better.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
Sergey Kucher
  • 4,140
  • 5
  • 29
  • 47