J
Jon Slaughter
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;
};
};
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:
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:
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;
};
};