Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Virtual Template Member Function - Design Workaroun

Reply
Thread Tools

Virtual Template Member Function - Design Workaroun

 
 
joe
Guest
Posts: n/a
 
      05-22-2006
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);

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-22-2006
* 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?
 
Reply With Quote
 
 
 
 
joe
Guest
Posts: n/a
 
      05-22-2006
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);

 
Reply With Quote
 
joe
Guest
Posts: n/a
 
      05-22-2006
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);

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-22-2006
* 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?
 
Reply With Quote
 
joe
Guest
Posts: n/a
 
      05-22-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
specialized member function takes precedence over generic template member function bluekite2000@gmail.com C++ 1 07-20-2005 08:58 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM



Advertisments