Menu

Functor to const member function

Developers
2006-11-06
2013-04-08
  • Nobody/Anonymous

    I get an error regarding Call of nonfunction on line 526 in Functor.h (loki-0.1.4\loki-0.1.4\include\noncc\Borland)
    anyone who know why? Or someone who knows a proper way to implement functors to const member functions.

    struct SomeClass
    {
       std::string value;
       void set(std::string const & p)
       {
          value = p;
       }
       std::string get() const
       {
          return value;
       }
       std::string get()
       {
          return value;
       }
    };

    void TestFunction(int, double)
    {
       int i = 4;
    }

    void TestFunction(int)
    {
       int i = 5;
    }

    typedef std::string (SomeClass::* const_getter)() const;

    typedef void (*ptTestFunction)(int);

    typedef Loki::Functor<void, TYPELIST_1(std::string const &)> setter;
    typedef Loki::Functor<std::string> getter;

    #pragma argsused
    int main(int argc, char* argv[])
    {
       SomeClass * object = new SomeClass();
       SomeClass const * cobject = new SomeClass();

       const_getter tmp = &SomeClass::get;
       setter f1(&(*object), &SomeClass::set);

       std::string test = "HelloWorld";

       ptTestFunction t = &TestFunction;
       Loki::Functor<void, TYPELIST_1(int)> get (t);

       Loki::Functor<std::string> t2(tmp);

       f1(test);

       return 0;
    }

    /t

     
    • Nobody/Anonymous

      And to clearify, it works if the get function isn't overloaded but neither by using initialization or by static_cast. (i looked at the examples in the book about overloading)

      /t

       
    • Peter Kuemmel

      Peter Kuemmel - 2006-11-06

      To construct a Functor with a member function you also need a object on which the member function could be called:

      Loki::Functor<std::string> t2(object, tmp);

       
    • Nobody/Anonymous

      Thanks, I thought I had tried that. But evidently not.

      /t

       

Log in to post a comment.