Virtual Template Member Function - Design Workaroun

Discussion in 'C++' started by joe, May 22, 2006.

  1. joe

    joe Guest

    Clearly I can't have a virtual template member function.

    What I want to do is pass input of _any_ type, and operate on it in a
    finite number of ways

    what I really want to do is:

    class DataHolder
    {

    template<typename T>
    void execute(T* input) {

    handler->execute(input)
    }
    Handler *handler;
    };


    class Handler
    {
    template<typename T>
    virtual void execute(T* input) = 0; // Have the derived types handle
    this call <--Not C++

    };

    Where I have many Handlers who mostly act the same, and have most of
    the same functions (I really want Inheritance here).

    I don't know the input type or the Handler type until runtime, so
    neither class can be templated.

    Any ideas? The goal is to have another user be able to write code:
    DataHolder dataHolder;
    dataHolder.execute(myFavType* data);
     
    joe, May 22, 2006
    #1
    1. Advertising

  2. * joe:
    > Any ideas? The goal is to have another user be able to write code:
    > DataHolder dataHolder;
    > dataHolder.execute(myFavType* data);


    That's not valid C++ syntax.

    If you can specify more precisely what the goal is, there might be a
    simple solution.



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 22, 2006
    #2
    1. Advertising

  3. joe

    joe Guest

    User Code: (in proper syntax)

    int *data = new data[50];
    DataHolder dataHolder; <-- In here, we set it's member handler ptr to
    the proper derived type under the covers
    dataHolder.execute(data); <--- Want to call template<typename T>
    DerivedHandler::execute(T* data);
     
    joe, May 22, 2006
    #3
  4. joe

    joe Guest

    User Code: (in proper syntax)

    int *data = new int[50];
    DataHolder dataHolder; <-- In here, we set it's member handler ptr to
    the proper derived type under the covers
    dataHolder.execute(data); <--- Want to call template<typename T>
    DerivedHandler::execute(T* data);
     
    joe, May 22, 2006
    #4
  5. * joe:
    > User Code: (in proper syntax)
    >
    > int *data = new data[50];
    > DataHolder dataHolder; <-- In here, we set it's member handler ptr to
    > the proper derived type under the covers


    Does that handler type depend on the 'data' type? If so, how is it
    communicated to the dataHolder object at this point?


    > dataHolder.execute(data); <--- Want to call template<typename T>
    > DerivedHandler::execute(T* data);


    It seems the missing piece here is the connection between the 'data'
    type and the 'DerivedHandler' type: what is it?


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 22, 2006
    #5
  6. joe

    joe Guest

    There is absolutely no connection between data type and HandlerType.
    Perhaps it is simpler to think of it as a communication. I want to
    send data of any type *somehow*. That somehow is determined only at
    runtime in a configuration file. Perhaps I want to record the data to
    disk. Perhaps I want to send it out over the Internet. That is what
    the DerivedHandler will give me. The ability to set this up in advance
    without having to do some sort of if/else switch at runtime.
     
    joe, May 22, 2006
    #6
    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. Replies:
    1
    Views:
    475
    Victor Bazarov
    Jul 20, 2005
  2. Replies:
    4
    Views:
    371
    mlimber
    Jan 23, 2006
  3. Replies:
    11
    Views:
    698
    James Kanze
    Sep 10, 2006
  4. Replies:
    7
    Views:
    590
    James Kanze
    May 2, 2007
  5. Peng Yu
    Replies:
    3
    Views:
    781
    Thomas J. Gritzan
    Oct 26, 2008
Loading...

Share This Page