Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector of generic function pointers

Reply
Thread Tools

vector of generic function pointers

 
 
inquisitive
Guest
Posts: n/a
 
      02-27-2008
Is it possible to have a vector of generic function pointers?

I am able to devise a generic function pointer, this way:

template <typename elemType, elemType (*function)(std::string&)>
struct Method
{
inline elemType operator()(std::string & property)
{
return function(property);
}
};

It is instantiated as:
Method <int, methodOne> numberMethod;

Now I want to create a vector of these generic function pointers, how
do I declare such a vector and how do I initialize its elements:

I found this Scott Meyer's link, which maybe helpful - it is giving me
hint but I am not able to gain my solution from it: http://www.ddj.com/cpp/184401580
 
Reply With Quote
 
 
 
 
Triple-DES
Guest
Posts: n/a
 
      02-27-2008
On 27 Feb, 08:04, inquisitive <(E-Mail Removed)> wrote:
> Is it possible to have a vector of generic function pointers?
>
> I am able to devise a generic function pointer, this way:
>
> template <typename elemType, elemType (*function)(std::string&)>
> struct Method
> {
> * * inline elemType operator()(std::string & property)
> * * {
> * * * * return function(property);
> * * }
>
> };
>
> It is instantiated as:
> *Method <int, methodOne> numberMethod;
>
> Now I want to create a vector of these generic function pointers, how
> do I declare such a vector and how do I initialize its elements:


You could make a common base class:

template<typename E>
struct MethodBase
{
virtual E operator()(const std::string&) const = 0;
};
template<typename E, E(*f)(const std::string&)>
struct Method : public MethodBase<E>
{
// ...
};

int main()
{
std::vector<MethodBase<int> *> v;
v.push_back( new M<int, someFunction>);
v.push_back( new M<int, someFunction2>);
// cleanup
}
 
Reply With Quote
 
 
 
 
inquisitive
Guest
Posts: n/a
 
      02-27-2008

On Feb 27, 2:07 pm, Triple-DES <(E-Mail Removed)> wrote:
> On 27 Feb, 08:04, inquisitive <(E-Mail Removed)> wrote:
>
>
>
> > Is it possible to have a vector of generic function pointers?

>
> > I am able to devise a generic function pointer, this way:

>
> > template <typename elemType, elemType (*function)(std::string&)>
> > struct Method
> > {
> > inline elemType operator()(std::string & property)
> > {
> > return function(property);
> > }

>
> > };

>
> > It is instantiated as:
> > Method <int, methodOne> numberMethod;

>
> > Now I want to create a vector of these generic function pointers, how
> > do I declare such a vector and how do I initialize its elements:

>
> You could make a common base class:
>
> template<typename E>
> struct MethodBase
> {
> virtual E operator()(const std::string&) const = 0;};
>
> template<typename E, E(*f)(const std::string&)>
> struct Method : public MethodBase<E>
> {
> // ...
>
> };
>
> int main()
> {
> std::vector<MethodBase<int> *> v;
> v.push_back( new M<int, someFunction>);
> v.push_back( new M<int, someFunction2>);
> // cleanup
>
> }


Thank you, but I want the independence to have different return-types
for my functions.

The problem that I was facing is to as to how could I templatize the
parameter passed on to the vector at its declaration.
My core issue is to create a generic function pointers with the
signature known in advance but not the actual type of the parameter
and return value. And then how do I build a collection of such generic
function pointers.

A second question is, if the vector and the functions are part of a
class, how would the declaration look like.
 
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
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
can I use stl vector iterator to delete a vector of pointers? zl2k C++ 27 09-07-2010 11:47 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM



Advertisments