Functors

Discussion in 'C++' started by Jon Slaughter, Jun 6, 2007.

  1. 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;
    };
    };
     
    Jon Slaughter, Jun 6, 2007
    #1
    1. Advertising

  2. Jon Slaughter

    Alan Johnson Guest

    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, Jun 6, 2007
    #2
    1. Advertising

  3. "Alan Johnson" <> wrote in message
    news:...
    > 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
     
    Jon Slaughter, Jun 6, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul MG
    Replies:
    2
    Views:
    395
    Dhruv
    Jul 3, 2003
  2. Rob Williscroft

    Re: Need help with generic functors....

    Rob Williscroft, Aug 17, 2003, in forum: C++
    Replies:
    3
    Views:
    347
    Gordon Scott
    Aug 18, 2003
  3. red floyd
    Replies:
    0
    Views:
    321
    red floyd
    Nov 13, 2003
  4. nsgi_2004

    functors

    nsgi_2004, Aug 12, 2004, in forum: C++
    Replies:
    2
    Views:
    468
    Karl Heinz Buchegger
    Aug 12, 2004
  5. Satish
    Replies:
    1
    Views:
    790
    David Hilsee
    Sep 10, 2004
Loading...

Share This Page