Communication function failed

I use boost (signal + bind) and C ++ to pass a reference to a function. Here is the code:

#define CONNECT(FunctionPointer) \
        connect(bind(FunctionPointer, this, _1));

I use it as follows:

class SomeClass {
  void test1() {}
  void test2(int someArg) {}

  SomeClass() {
     CONNECT(&SomeClass::test1);
     CONNECT(&SomeClass::test2);
  }
};

The interaction of the second test function (test2), because it has at least one argument. On the first test, I have an error:

‘void (SomeClass::*)()’ is not a class, struct, or union type

Why can't I pass functions without arguments?

+3
source share
1 answer

_1is a placeholder argument that means "replace the first input argument". The method test1has no arguments.

Create two different macros:

#define CONNECT1(FunctionPointer) connect(bind(FunctionPointer, this, _1));
#define CONNECT0(FunctionPointer) connect(bind(FunctionPointer, this));

But remember, macros are evil .

And use it as follows:

class SomeClass {
  void test1() {}
  void test2(int someArg) {}

  SomeClass() {
     CONNECT1(&SomeClass::test1);
     CONNECT0(&SomeClass::test2);
  }
};
+4
source

Source: https://habr.com/ru/post/1756165/


All Articles