Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I have: 1 hypothetical object/class + other classes and related methods that gives me functionality.

What I want:

  1. linking this object to 0 to N methods in realtime on request when an event is triggered
  2. Each event is related to a single method or a class, so a single event does not necessarily mean "connect this 1 method only" but can also mean "connect all the methods from that class or a group of methods"
  3. Avoiding linked lists because I have to browse the entire list to know what methods are linked, because this does not ensure me that the linked methods are kept in a particular order (let's say an alphabetic order by their names or classes), and also because this involve a massive amount of pointers usage.


I have an object Employee Jon, Jon acquires knowledge and forgets things pretty easily, so his skills may vary during a period of time, I'm responsible for what Jon can add or remove from his CV, how can I implement this logic?

share|improve this question
up vote 1 down vote accepted

With regard to 1. and 2.

What you're describing doesn't really sound like a 'state machine' to me (And i'm afraid that I couldn't make much sense of the example in the context of what you described).
A state machine typically involves transitioning between known states when a a condition is hit for that particular state; it sounds more like you're trying to store a list of event subscriptions which may change arbitrarily over time - for this you probably need something like the Observer pattern.

With regard to 3.

I have never encountered any situation in C++ where a hand-written linked list is at all desirable; you should prefer the standard libraries as a general rule-of-thumb. std::deque or std::map might be a good starting point as a means to storing subscribers/observers.

Using a modern C++11 compiler, you could implement a std::map< std::string, std::function<T>> for each of the events which you wish to subscribe to (with the string/key being used to identify the observer's target for later removing or modifying the subscription).

std::function< T > lets you use any kind of callable object, lambda, function or curried function binder as the subscriber, whose callable signature matches T.

for example, with std::function< void() > you could reasonably use

std::function<void()> fred = [](){ std::cout << "hello world"; };
fred();  // call the lambda


struct foo {
    void operator() () { std::cout << "hello world"; }

std::function<void()> bob = foo();
bob();  // call the functor


void foo() { std::cout << "hello world"; }

// ---------------------------------------

    std::function<void()> blah = foo;
    blah();   // call the function


class MyClass
    void func() { std::cout << "hello world" << std::endl; }

// -----------------------------------------------------

    MyClass obj;
    std::function<void()> meow = std::bind( &MyClass::func, obj );

    meow();  // Call the member function using the object

From this, It should be fairly easy to imagine a container full of std::function objects. Of course, you could also wrap std::function in another class if needed (which would take you a little closer to the GoF Observer Pattern).

share|improve this answer
I'm thinking a lot about pointers, in my view the state of the machine is an answer to the question "Is that pointer containing the address of the method or is NULL?", in fact my first approach was about a multidimensional array of pointers basically, but it would be much worst than a list in my case. I also want to avoid the use of the templates in C++ because this impact on the organization of my source code, especially when dividing the header from the actual implementation. I'm actually reading something about what you suggest from the std:: world, i'm posting something later. – user827992 Jun 23 '12 at 14:23
I'm afraid I don't really understand the comment. Perhaps you could elaborate a little bit in your original question with a more complete explanation of what you're actually doing? i.e. why do you care about pointers? Also, what 'impact' on the organisation of your code are you referring to? – Ben Cottrell Jun 23 '12 at 15:21
As generic idea my reference is a node-editor, something like the ones used in many 3D-related software. The problem regarding the templates is due to the fact that i want to separate the interfaces from the actual definition and implementation. – user827992 Jun 23 '12 at 15:38
You don't need to write template code in order to use the C++ standard libraries, nor do you have to change anything about the organisation of your code to use containers such as std::deque or std::map. I'm not sure where you got the idea from that you won't be able to separate your interface from implementation. – Ben Cottrell Jun 23 '12 at 15:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.