A typedef that refers to itself

前端 未结 2 1416
一个人的身影
一个人的身影 2021-01-11 10:44
typedef bool (*Foo)(Foo a, Foo b);

How do you declare a function pointer that accepts itself in its parameters?

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 11:01

    Indirectly:

    struct Foo{
      typedef bool (*FooPtr)(Foo a, Foo b);
      Foo(FooPtr p)
          : p(p)
      {}
    
      bool operator()(Foo a, Foo b) const{
        return p(a,b);
      }
    
      FooPtr p;
    };
    
    struct Bar{
        Bar(Foo f)
            : some_callback(f)
        {}
    
        Foo some_callback;
    };
    
    bool a_callback(Foo a, Foo b){
        return false;
    }
    
    int main() {
        Bar b(a_callback);
        b.some_callback(Foo(a_callback), Foo(a_callback));
    }
    

    Not that I could ever see any use in that, as you can see from my example.

提交回复
热议问题