0

I have a functor defined within a class. I want to access member function of the outer class directly from the functor. How do I do that? I want to pass this functor to a template class elsewhere in the code. I dont want to use function pointers.

Does the functor need to keep a reference to the internal class as a member, assigned to it at the time of initilization, to be able to call that class's function?

  Class MyClass //outer class
    {
    void DoSomething() //member function I want to access from functor
    {
    }

    class MyFunctor //the functor who wants to access outerclass's function
    {
    void operator() ()
    {
    DoSomething() //This is a member function of outer class
    }
    }   

    };

}

say if I want to keep a local reference to the outer class inside the functor then how do I create an instance (not a pointer) to the functor and pass the pointer to the outerclass in functor's constructor?

bsobaid
  • 955
  • 1
  • 16
  • 36
  • for accessing methods of a class you must take an object of that class. But do you think of static methods? – inverted_index Feb 01 '16 at 17:30
  • DoSomething() cant be a static method. So Functor must have a local reference to the outerclass as a member to call the outer class's function.? and there is no other way? – bsobaid Feb 01 '16 at 17:35
  • So i think you're right. You should have a refrence to the outerclass. This refrence is an argument that pass to a Functor's method ( `operator()` in your case ). Thus declaration of operator method should be like this: `void MyFunctor::operator(MyClass* outer){ outer->DoSomething(); }` – inverted_index Feb 01 '16 at 17:49
  • I hope this link would help you. Take a look Here: http://www.dreamincode.net/forums/topic/61757-calling-member-functions-from-another-class/ – inverted_index Feb 01 '16 at 17:52
  • so i guess if I have to pass *outer and call a member DoSomething() of outer then no need to use functor. I was planning to pass functor to a class thats basically a utility so that utility does'nt really know of any context or outside logic other than its own logic. thats why I wanted to pass the functor, so utility can remain a utility. With this approach I will need to add logic and context inside that utility class which I Was trying to avoid. – bsobaid Feb 01 '16 at 17:55

0 Answers0