Factory for objects w/o default constructor (tmp?)

Discussion in 'C++' started by Shawn McGrath, Jan 6, 2007.

  1. I'm writing a generic message handler system with two basic classes:
    template <typename T>
    struct MessageHandler //singleton-ized elsewhere
    {
    ...
    void publish(...);
    void subscribe(MessageInterface<T> *);
    void unsubscribe(MessageInterface<T> *);
    private:
    std::vector<MessageInterface<T> *> vec;
    };

    and obviously:
    template <typename T>
    struct MessageInterface
    {
    ...
    void receiveMessage(const T *msg);
    };

    right now MessageHandler::publish() looks like:
    publish(T *msg) { std::for_each(vec... receiveMessage...);

    This works fine, however it's up to the caller of publish() to create
    the message. I'd rather put message creation inside the MessageHandler
    class. The difficulty with doing that is not all T's will have a
    default constructor.

    I was looking into how boost::signals work, but that defines the
    arguments at the time of handler class creation, and it's overall quite
    confusing - is there a simpler way?

    If not, can I apply the boost::signal method to make publish() a
    template member function - if yes, is there a way to automatically
    infer the template parameters based off the arguments? (ie so I don't
    have to do:

    message_handler.publish<int>( x );

    and just have it know that I'm using publish<int> by the type of the
    argument passed to publish())

    Thanks a lot.
    -Shawn.
     
    Shawn McGrath, Jan 6, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Medi Montaseri
    Replies:
    17
    Views:
    903
    Medi Montaseri
    Sep 3, 2003
  2. Aire
    Replies:
    3
    Views:
    483
    Mike Wahler
    Jan 25, 2004
  3. Replies:
    9
    Views:
    987
    Alf P. Steinbach
    Mar 6, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,315
  5. C#
    Replies:
    4
    Views:
    426
Loading...

Share This Page