0

I have an istance of a class A, that has some methods like:

class A
{
 ...
public:
 bool  DoOperation_one(Mytype*  pSomeValue);
 bool  DoOperation_two(Mytype*  pSomeValue);
 ... 
 bool  DoOperation_th(Mytype*  pSomeValue);
...
}

Another class, class B, has a pointer to A class and a method BMethod.

Class B
{
 ...
 A*  myPtrA;
...

bool  BMethod(...);   // arguments have to be defined
}

Is it possible to pass to BMethod, methods of A class instance as argument? I will try to be more clear with the follow pseudocode. In same place in class B i call BMethod with myPtrA method as argument (with parameters). I don't want to execute the myPtrA->DoOperation_two(somevalue) at calling time, but only in the BMethod (if statement):

    ...
    bool bVal = BMethod(myPtrA->DoOperation_two(somevalue))
    ...

bool B::BMethod("valid signature" myFunction)
{
 bool bfun=false;

  if (myFunction)
  {
    do_something....
  }

 return  bfun;
}

My goal is to call BMethod argument (DoOperation_one, DoOperation_two or DoOperation_th) in the body of BMethod only. I have been used template, but DoOperation_XXX is executed at calling time.

....
bool bVal = BMethod(myPtrA->DoOperation_two(somevalue));

or

bool bVal = BMethod(myPtrA->DoOperation_one(somevalue));

...

     template <typename FunctionT>
         bool BMethod(FunctionT myFunc)
    {
          bool bfun=false;
          bool breturn = false;
    
          while (!bfun)
          {
            if (myFunc)
             {
              bfun=true;
              breturn = some_other_operation();
             }
           }
    
         return  breturn ;
    }

Moreover, myFunc contains result of myPtrA->DoOperation_one(somevalue), and doesn't change in the while statement because it has executed at calling time.

Is there any other approach/solution?

cigien
  • 57,834
  • 11
  • 73
  • 112
MrMartins
  • 93
  • 5
  • 2
    `bool bVal = BMethod(myPtrA->DoOperation_two(somevalue));` is passing the value returned from the method call to `BMethod`. Please clarify if you really need to pass the function or why simply passing its return value is not sufficient – 463035818_is_not_an_ai Apr 13 '22 at 11:15
  • 2
    `BMethod` would need the function and the parameter to call it. It is not clear what you mean when you write `if (myFunc)`. Is this supposed to call the function? Do you need to call the function in the loop (maybe because this is multithreaded and the value returned may change) ? – 463035818_is_not_an_ai Apr 13 '22 at 11:18
  • With bool bVal = BMethod(myPtrA->DoOperation_two(somevalue)) i need to pass function with its parameters – MrMartins Apr 13 '22 at 11:27
  • if (myFunc) : Here i call the function which will be called several times until some condition will be verified. – MrMartins Apr 13 '22 at 11:29
  • Your question isn't really that clear. *Maybe* you want to pass a variable function pointer as the first argument but, if so, [pointers to member functions have very tricky syntax](https://stackoverflow.com/q/2402579/10871073). – Adrian Mole Apr 13 '22 at 11:32
  • i would like to pass pointer of instance methods. My goal is to use the same BMethod for all the methods of A Class (which have the same signature: bool DoOperation ...). PS: sorry for my unclear question – MrMartins Apr 13 '22 at 11:49
  • OK - Then I guess it *is* a duplicate ... – Adrian Mole Apr 13 '22 at 11:56

1 Answers1

1

You can pass a member function pointer but then you'll need an A instance to call it and a Mytype* to be passed as parameter:

struct Mytype {};

struct A {
    bool  DoOperation_one(Mytype*  pSomeValue) { return true;}
    bool  DoOperation_two(Mytype*  pSomeValue) { return true;}
    bool  DoOperation_th(Mytype*  pSomeValue) { return true; }
};

struct B {
    A  a;
    template <typename F>
    bool  BMethod(F f,Mytype* mt) {
        bool bfun=false;
        bool breturn = false;
    
        while (!bfun) {
            if ((a.*f)(mt))
             {
              bfun=true;
              breturn = true;//some_other_operation();
             }
           }
    
         return  breturn ;
    }
};
int main() { 
    Mytype mt;
    B b;
    b.BMethod(&A::DoOperation_one,&mt);
}

Instead of making BMethod a template, I could have spelled out the type explcitly, it is bool (A::*)(Mytype*). The syntax to call a member function pointer is a little clunky. If a is a pointer to A (like in your code) then it is (a.->f)(mt).

Note that this only makes sense if the value returned from A::DoOperation_xyz changes while B::BMethod is executing. For example when multiple threads are involved. If thats the case you need to be careful with synchronizing access to shared data to avoid data races. If this isnt multi-threaded, I see no reason to make it complicated and BMethod can simply be bool BMethod(bool x).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185