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 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::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;
};
}; 2 1730
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
"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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: red floyd |
last post by:
Disclaimer: VS.NET 2003 (haven't checked any other compiler).
I'm writing functors for my classes.
Because the objects in my containers are large, I'm making my functors take const T& parameters....
|
by: AngleWyrm |
last post by:
I have created a new container class, called the hat.
It provides random selection of user objects, both with and without
replacement, with non-uniform probabilities. Uniform probabilities are a...
|
by: nsgi_2004 |
last post by:
Hi,
I have been learning about functors and at first everything was clear. Make
a class and overload operator () so that an object of the functor can be
thought of as a function.
However, I...
|
by: Matthias |
last post by:
Hello,
I basically want to implement a general resource manager which should have
a caching and a loading policy so that I can exchange those. Here's some
example code of how it should work:
...
|
by: Fraser Ross |
last post by:
Functors taking 1 argument for operator() should inherit from
unary_function and those with 2 arguments should inherit from
binary_function. If a functor has zero arguments for its operator()...
|
by: michael.lesniak |
last post by:
Hello,
I'm learning C++ for a couple of days and play a bit with the
algorithms provided in the STL. One thing I don't understand is the
fact that classes inherited of functors have to be...
|
by: sushobh |
last post by:
Hello All,
Just want to put up a query with regards to functors.
I read that Functor means a function with state. But what I am not able to understand is the following
, What is the sate ...
|
by: tryptik |
last post by:
Hello all,
I have a question about iterators. I have a container of functors
that operate on an std::string. The functors go something like this:
class Functor {
std::string...
|
by: Christopher |
last post by:
I used to just use a plain old function pointer is a call to
std::sort.
My colleagues are telling me that I need to use a "functor". Ok, I
google and see that a functor is a class with a public...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |