Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Functors

Reply
Thread Tools

Functors

 
 
Jon Slaughter
Guest
Posts: n/a
 
      06-05-2007
I'm trying to mess with functors and the way I want it to work is that when
I create a functor it will automatically add itself to an array.

The attached code demonstrates what I mean. The problem is that its a bit
unsatisfactory and I'd like to improve it.

In the constructor of TClassA

Functors<TClassA, std::string> *Functor = new Functors<TClassA,
std::string>(this, &TClassA:isplay);

So now I every time I want a new functor I have to change the name 4 spots
;/ Is there some way I can extra the name so I don't have to do this? (So
essentially the class name gets inserted above, or at the very least it some
othe rmethod that would work)


My problem is that I really just want to create a set of "transformations"
of a string(well, for sake of argument) but have those transformations be
put in an array. Its very simple to do with functions and function pointers
but I figured that I would try to do it with functors. Actually the way it
will work is that a vector or list will contain the functors and the
transformations will have a probability associated with them which will
determine how likely they are called(or applied)

for example,

say I have 3 transformations of a alpha-numeric string,

T1 = doubles every character,
T2 = removes the first 3 characters,
T3 = Addes x to every character in the set {s,t,v}

T1 has probability of 1/2 being called, T2 has probability 1/4 and T3
probability 1/4.

So I have a function that picks the functors with their probability and
applies them to a string transforming it.

(essentially just picking a random number between 0 and 1 and if it falls
within 1/2 then it calls T1, if 1/2 to 3/4 it calls T2 else T3)

I can then repeat this as many times as I'd like.

The issue is that I will need to create a lot of transformation functions
and I'm just curious if I can simplify the method I'm using)

Thanks,
Jon



template<class type>
class Functor
{
public:
virtual type operator()(type &)=0; // call using operator
};


template <class TClass, class type>
class Functors : public Functor<type>
{
private:
type (TClass::*fpt)(type &); // pointer to member function
TClass* pt2Object; // pointer to object
public:
Functors(TClass* _pt2Object, type (TClass::*_fpt)(type &))
{
pt2Object = _pt2Object;
fpt=_fpt;
};
// override operator "()"
virtual type operator()(type &v)
{
return (*pt2Object.*fpt)(v);
};
};

std::vector< Functor<std::string>* > vTab;
class TClassA{
private:
int prob;
public:
TClassA(int t)
{
prob = t;
Functors<TClassA, std::string> *Functor = new Functors<TClassA,
std::string>(this, &TClassA:isplay);
vTab.push_back(Functor);
};
std::string Display(std::string &v)
{
std::string str;
std::stringstream out;
out << prob;
str = out.str();
v.insert(0, str);
return v;
};
};


 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      06-06-2007
Jon Slaughter wrote:
> I'm trying to mess with functors and the way I want it to work is that when
> I create a functor it will automatically add itself to an array.
>
> The attached code demonstrates what I mean. The problem is that its a bit
> unsatisfactory and I'd like to improve it.
>
> In the constructor of TClassA
>
> Functors<TClassA, std::string> *Functor = new Functors<TClassA,
> std::string>(this, &TClassA:isplay);


Create a function template to do the work for you. The template
parameter can be inferred from the type passed to the function. Example:

template <class T>
void addFunctor(T * pt)
{
vTab.push_back(new Functors<T, std::string>(pt, &T:isplay));
}

Your constructor then becomes:
TClassA(int t)
: prob(t)
{
addFunctor(this);
}

Your original code:
>
> template<class type>
> class Functor
> {
> public:
> virtual type operator()(type &)=0; // call using operator
> };
>
>
> template <class TClass, class type>
> class Functors : public Functor<type>
> {
> private:
> type (TClass::*fpt)(type &); // pointer to member function
> TClass* pt2Object; // pointer to object
> public:
> Functors(TClass* _pt2Object, type (TClass::*_fpt)(type &))
> {
> pt2Object = _pt2Object;
> fpt=_fpt;
> };
> // override operator "()"
> virtual type operator()(type &v)
> {
> return (*pt2Object.*fpt)(v);
> };
> };
>
> std::vector< Functor<std::string>* > vTab;
> class TClassA{
> private:
> int prob;
> public:
> TClassA(int t)
> {
> prob = t;
> Functors<TClassA, std::string> *Functor = new Functors<TClassA,
> std::string>(this, &TClassA:isplay);
> vTab.push_back(Functor);
> };
> std::string Display(std::string &v)
> {
> std::string str;
> std::stringstream out;
> out << prob;
> str = out.str();
> v.insert(0, str);
> return v;
> };
> };
>
>


--
Alan Johnson
 
Reply With Quote
 
 
 
 
Jon Slaughter
Guest
Posts: n/a
 
      06-06-2007

"Alan Johnson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> Jon Slaughter wrote:
>> I'm trying to mess with functors and the way I want it to work is that
>> when I create a functor it will automatically add itself to an array.
>>
>> The attached code demonstrates what I mean. The problem is that its a bit
>> unsatisfactory and I'd like to improve it.
>>
>> In the constructor of TClassA
>>
>> Functors<TClassA, std::string> *Functor = new Functors<TClassA,
>> std::string>(this, &TClassA:isplay);

>
> Create a function template to do the work for you. The template parameter
> can be inferred from the type passed to the function. Example:
>
> template <class T>
> void addFunctor(T * pt)
> {
> vTab.push_back(new Functors<T, std::string>(pt, &T:isplay));
> }
>
> Your constructor then becomes:
> TClassA(int t)
> : prob(t)
> {
> addFunctor(this);
> }
>



Thanks, I'll try that and see.

Jon


 
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
Deleting a map of pointers with functors Satish C++ 1 09-10-2004 01:40 AM
functors nsgi_2004 C++ 2 08-12-2004 07:35 AM
functors with reference parameters red floyd C++ 0 11-13-2003 12:50 AM
Re: Need help with generic functors.... Rob Williscroft C++ 3 08-17-2003 11:57 PM
Functors with behaviour in the destructor Paul MG C++ 2 07-03-2003 03:12 AM



Advertisments