P: n/a

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::Display);
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 alphanumeric 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::Display);
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;
};
};  
Share this Question
P: n/a

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::Display);
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::Display));
}
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::Display);
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  
P: n/a

"Alan Johnson" <aw***@yahoo.comwrote in message
news:YM******************************@comcast.com. ..
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::Display);
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::Display));
}
Your constructor then becomes:
TClassA(int t)
: prob(t)
{
addFunctor(this);
}
Thanks, I'll try that and see.
Jon   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1386
 replies: 2
 date asked: Jun 5 '07
