PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > vector of generic function pointers
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
vector of generic function pointers

Réponse
 
LinkBack Outils de la discussion
Vieux 27/02/2008, 07h04   #1
inquisitive
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut vector of generic function pointers

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 ful - it is giving me
hint but I am not able to gain my solution from it: http://www.ddj.com/cpp/184401580
  Réponse avec citation
Vieux 27/02/2008, 09h07   #2
Triple-DES
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: vector of generic function pointers

On 27 Feb, 08:04, inquisitive <khattri.vaib...@gmail.com> 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
}
  Réponse avec citation
Vieux 27/02/2008, 10h14   #3
inquisitive
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: vector of generic function pointers


On Feb 27, 2:07 pm, Triple-DES <DenPlettf...@gmail.com> wrote:
> On 27 Feb, 08:04, inquisitive <khattri.vaib...@gmail.com> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h49.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14268 seconds with 11 queries