Virtual Template Member Function - Design Workaroun

J

joe

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);
 
A

Alf P. Steinbach

* 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.
 
J

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
dataHolder.execute(data); <--- Want to call template<typename T>
DerivedHandler::execute(T* data);
 
J

joe

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);
 
A

Alf P. Steinbach

* 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?
 
J

joe

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top