-1

I have a base class which has a method that takes a method pointer (class function pointer) as an argument. I derive a child class from this base class. I then want to pass a method of the child class to the method of the base class.

An example:

    class Base1
    {
        public:
             void BaseMethod(void (Base1::*basemth)(/*arguments list A*/), 
                             double a)){/*definition*/}
    };
    class Derived1 : public Base1
    {
        public: 
             void ChildMethod(/*arguments list A*/)

             void ChildMethod2()
             {
                double double_a = 1.0;
                void (Derived1::*pntr)(/*arguments list A*/);
                pntr = &Derived1::ChildMethod;
                BaseMethod(pntr, double_a); //<---- this fails
             }

    };

I get "error: no matching function for call to...".

Is this possible? If it is, what am I doing wrong? Any help will be appreciated. I have looked at child class method pointer to method parent argument c++ but following R Sahu's method does not seem to work, while Slava's answer does not seem relevant (I am not calling a child method from a parent instance).

Community
  • 1
  • 1
Chris
  • 13
  • 4
  • 2
    Are those tick marks on line 5 accidental? Also, what exactly is the complete error message text? – donjuedo Jun 08 '15 at 18:39
  • Agreed, if there is problem with whatever your error is (couldn't make sense of it,) then you are really going to have issues with the lines to follow. – Evan Carslake Jun 08 '15 at 18:40
  • Yes, the code included here is an example. I do apologise if the design is ugly @Ed Heal - I have not had a formal training in OOP. The actual error message is :"error: no matching function for call to ‘Derived1::Loop(void (Derived1::*&)(std::vector >, std::map, double>&), std::vector >&, std::map, double>&, int, int, int, int)’ BaseMethod(pntr, sq, args, zno - 2, xno - 2, yno - 2, 0);" where "sq, args, zno - 2, xno - 2, yno - 2, 0" are the arguments I mentioned earlier. – Chris Jun 08 '15 at 18:46
  • The tick marks are accidental, yes (first post mishap) @donjuedo – Chris Jun 08 '15 at 18:51

1 Answers1

0

Is this possible?

It is possible and should work ok if you use a virtual member function. If you use a non-virtual member function, it won't work.

Example program:

#include <iostream>

class Base1
{
   public:

      void BaseMethod(void (Base1::*basemth)(), double a)
      {
         (this->*basemth)();
      }

      virtual void Method2() = 0;

};

class Derived1 : public Base1
{
   public: 

      void Method2()
      {
         std::cout << "In Derived1::Method2()\n";
      }

      void ChildMethod2()
      {
         double double_a = 1.0;
         BaseMethod(&Base1::Method2, double_a);
      }

};

int main()
{
   Derived1 d;
   d.ChildMethod2();
}

Output:

In Derived1::Method2()
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @R Sahu I have seen your comment and I am trying it out now. Please give me a couple of minutes – Chris Jun 08 '15 at 18:55
  • @R Sahu, genius. It worked. I was declaring the virtual method as a child method, whereas you highlighted I should have been declaring it as a parent method. Many thanks! – Chris Jun 08 '15 at 19:00
  • Many thanks to Ed Heal, donjuedo and Evan Carslake for looking at my question and asking useful questions as well – Chris Jun 08 '15 at 19:01