Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Containers of Generic Function Pointers

Reply
Thread Tools

Containers of Generic Function Pointers

 
 
inquisitive
Guest
Posts: n/a
 
      02-27-2008
Is there a way to create a collection of Method objects, for example a
vector or a list?

template <typename elemType>
struct Method
{
bool (*function)(elemType&);

template <typename elemType>
inline bool operator()(elemType & property)
{
return function(property);
}
};

so that I can do something like:

int main()
{
std::string property;

std::vector <Method> methodList;

Method method;

method.function = someMethod;

methodList.push_back(method);

method.function = someOtherMethod;

methodList.push_back(method);

std::vector<Method>::iterator it;

for(it = methodList.begin(); it != methodList.end(); it++)
{
std::cout << "Value: " << ((*it)(property)) << std::endl;
}

return 0;
}

bool someMethod(int & value)
{
value = 25;

return true;
}

bool someOtherMethod(std::string & value)
{
value = "Hello, World!";

return true;
}
 
Reply With Quote
 
 
 
 
Triple-DES
Guest
Posts: n/a
 
      02-27-2008
On 27 Feb, 13:45, inquisitive <(E-Mail Removed)> wrote:
> Is there a way to create a collection of Method objects, for example a
> vector or a list?


Not the way you want. See comments below. See also my previous reply.

>
> template <typename elemType>
> struct Method
> {
> * * bool (*function)(elemType&);
>
> * * template <typename elemType>
> * * inline bool operator()(elemType & property)
> * * {
> * * * * return function(property);
> * * }
>
> };
>
> so that I can do something like:
>
> int main()
> {
> * * std::string property;
>
> * * std::vector <Method> methodList;


Which Method? Methods with different elemTypes are different types.
You can not store different types directly in a vector.

>
> * * Method method;
>
> * * method.function = someMethod;


For this to work, method has to be of type Method<int>

> * * methodList.push_back(method);
>
> * * method.function = someOtherMethod;


Method is now assumed to be of type Method<std::string>, which it
isn't

> * * methodList.push_back(method);
>
> * * std::vector<Method>::iterator it;
>
> * * for(it = methodList.begin(); it != methodList.end(); it++)
> * * {
> * * * *std::cout << "Value: " << ((*it)(property)) << std::endl;


Now you assume that all Methods are of type Method<string>. Otherwise
this would fail.

> * * }
>
> * *return 0;
>
> }
>
> bool someMethod(int & value)
> {
> * * value = 25;
> * * return true;
> }
>
> bool someOtherMethod(std::string & value)
> {
> * * value = "Hello, World!";
> * * return true;
> }


Sorry I couldn't be of more help.

DP
 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      02-27-2008
inquisitive schrieb:
> Is there a way to create a collection of Method objects, for example a
> vector or a list?


You can try boost.function which will be in the next C++ standard:
http://www.boost.org/doc/html/function.html

However, the functions stored need to have the same or a compatible signature.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sentences long extremely and notation Polish reverse in writing about
 
Reply With Quote
 
inquisitive
Guest
Posts: n/a
 
      02-28-2008
Thanks to both of you.

I suspected that I was assuming something which is fundamentally not
possible, but got lost in the templates. Thanks for conforming.
 
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
Are sequence containers not a subset of general containers? Sebastian Mach C++ 5 10-06-2012 07:54 PM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Re: Why no generic function pointers? Ben Bacarisse C Programming 3 06-22-2008 12:22 AM
vector of generic function pointers inquisitive C++ 2 02-27-2008 10:14 AM
Containers of iterators vs. containers of references clark.coleman@att.net C++ 7 01-25-2008 01:37 PM



Advertisments