English 中文(简体)
C++ 采用非静态接口方法
原标题:C++ callback to non-static interface method

我有一个“A”类,从B类接口中称为非静态类别方法,其签名用以下功能点表示:

<代码>bool(B:*check)(int)

采用这种方法的有一套类别{C}执行B,每个类别都有与上述签字相匹配的多种方法。 因此,我需要对A对B的回击进行约束,而后者反过来又将从C中下放到选定的方法。

www.un.org/Depts/DGACM/index_spanish.htm 加入一些法典:

这是我所铭记的概况。 既然认识到这只是上述要求的一个实例,那么在《刑法》的组织中,任何东西都不是强制性的,或许是A类。

class B {
  public:
    bool check(int val) const {
       // call to a binded method with a given signature (sig) on a Cx class
       ...
    }
};

class C1: public B {
  ...
  // a method implementing the (sig) signature
  // another method implementing the (sig) signature
};


class C2: public B {
  ...
  // a method implementing the (sig) signature
  // another method implementing the (sig) signature
};

class A {
  public:
  ...
  private:
    bool result;
    int val;

    void call_check(B const& b) const {
      result = b.check(val);      
    }
  ...
};

It that possible in C++? Or equivalently, what would be a solution that allows A to know of class B only?

对我来说,为了这一非常具体的需求,我已经找到了解决办法。

最佳回答

<Massive Edit

我认为,根据某些重的类型预测,我会想到你想要的东西。

请注意,我确实建议采用这一技术。 尽管我的榜样发挥了作用,但我认为这里有一些重大陷阱,可能确实使事情变本加厉。

如前所述,<代码>A可接受适当签名和标的两种方法(B,或来自<代码>B的,例如<代码>C各类别),并在特定物体上采用特定方法。

<条码>B实际上没有任何方法,只有作为两个类别(<代码>C1和>的普通基级;<代码>C2。

下面是<代码>main,显示如何使用。 我没有执行两个<代码>。 SetCallback*** methods, as they re trivial.

class B
{   public: // Has nothing, only serves as base-class for C1 and C2.        
};

class C1: public B
{
    public: bool DoThis(int x) const { return true; }
};

class C2: public B
{
    public: bool DoThat(int x) const { return false; }
};

class A
{
    private:
        bool (B::*m_check)(int) const;
        B* m_Obj;

    public:
        void SetCallbackFunction(bool (B::*fnc)(int) const)
        {   m_check = fnc; }

        void SetCallbackObject(B* pB)
        {   m_Obj = pB;  }

        bool InvokeCallback(int val)
        {
            return (m_Obj->*m_check)(val);
        }
};

int _tmain(int argc, _TCHAR* argv[])
{
    A a;
    C1 c;

    bool (C1::*func)(int) const;
    bool (B::*b_func)(int) const;

    func = &C1::DoThis;
    b_func = (bool (B::*)(int) const)(func);   // Dangerous Typecasting.  Use caution!

    a.SetCallbackFunction(b_func);
    a.SetCallbackObject((B*)(&c));   // A simpler, safer typecast.

    a.InvokeCallback(5);  // Ends up calling C1::DoThis.

    _getch();
    return 0;
}
问题回答

您可以做的最简单的事不是使用成员的职能点,而是像<条码>功能/代码>(即<条码>/条码>或C++11)这样的更高顺序结构,并且用<条码>宾德进行登记(加之、<条码>boost或C++11)。

点对成员的职能是可用的,确实是你想要的。 然而,仅仅知道他们不是实际的“点人”,而是“点人”的,因为它是possible,因此,适当的盘问/召唤必须经过与可能具有较强父母的阶层有关的“变压”表。

简言之,你可以这样做。 如果你在模板中实施,你确实需要将目标负责人列入而不是<>>。 需要在扩大法典时执行目标班级负责人。 如果你作为非雇员执行,那么,请could提前宣布该成员的职能并开始工作。 或者,你可以简单地包括目标类别的头盔。

由于有多种目标功能,是,在实际具有约束力的时刻,你必须包括负责人(如果是模板实施,你不需要负责人):

class MyA {
public:
  bool foo1(int) const;
  bool foo2(int) const;
};

void MyFunc(void) {
  bool (MyA::*my_ptr_to_func)(int) const;
  my_ptr_to_func = &MyA::foo2;

  MyA my_a;

  // call it
  if((my_a.*my_ptr_to_func)(42))
  {
    // ...
  }
}

[UPDATE], based on your updated code, it seems like you merely want to make "bool B::check(int) const" to be "virtual" in the base class, and override/re-implement that function in the derived "C1" and "C2" classes?

是的,virtual功能将称为(C1C2各行各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各式各样。 具体来说是why,指点人对成员的职能不是exactly指点人,而是等>点人/m>。 (允许你要求在衍生课程中正确执行virtual。)

So, no fear: It will work, just put "virtual" on B::check(int) in the base.

It sounds like you want to use the observer pattern to allow A to hold a vector of function pointers of type bool (B::*check)(int) const.

Classes in {C} could thus register through the observer pattern to A. I don t see why you need an interface, B, explicitly if you use this form of pattern. The interface will be guaranteed by the vector of function pointers requiring the signature of your chosen function pointer.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?