English 中文(简体)
2. 如何登记具有基类的行业成员职能单位
原标题:How to register a derived class member function pointer with a base class

与虚拟成员职能相反,我需要一种解决办法,即每个级别履行的衍生职能可以登记,然后由基类进行。 (不仅是最衍生的执行)

为了做到这一点,我正在考虑为衍生产品提供一种机制,以登记其功能的基类,例如在衍生型建筑商期间。

不过,我对成员职能点人的论点感到不安。 我认为,Derived来自基地,应当自动投放this的点。

www.un.org/spanish/ecosoc 能否做到这一点,与我正在尝试的或我是否需要使用静态成员职能接近:void*static_cast?。

class Base
{
protected:
    typedef void (Base::*PrepFn)( int n );
    void registerPrepFn( PrepFn fn ) {};
}

class Derived : public Base
{
    Derived() {
        registerPrepFn( &Derived::derivedPrepFn );
    };

    void derivedPrepFn( int n ) {};

}

Compiler error:

error: no matching function for call to  Derived::registerPrepFn(void (Derived::*)(int)) 
note: candidates are:                  void Base::registerPrepFn(void (Base::*)(int)) 
最佳回答

这一模式可与https://en.wikipedia.org/wiki/Curiously_reent_template_pattern”rel=“nofollow noretinger”>Curiously Reentlate Format处理,即编纂成像。

下面的法典将无一成文汇编。

template <class D>
class Base {
    protected:
        typedef void (D::*PrepFn) (int n);
        void registerPrepFn(PrepFn fn) {}
        Base() {};
        Base(PrepFn fn) {
            registerPrepFn(fn);
        }
};

class Derived : public Base<Derived>{
    Derived() {
        registerPrepFn( &Derived::derivedPrepFn);
    }
    void derivedPrepFn( int n) {};
};

class Derived2 : public Base<Derived2> {
    Derived2() : Base(&Derived2::derived2PrepFn) {}
    void derived2PrepFn(int n) {}
};

对我来说,带<条码>的版本越好,因为你将登记推向基类。 通过删除缺省代码的构造(此处为Derived<>/code>),你可以force<>/em>衍生出的类别,以提供准确的签字的点对成员功能。

问题回答

如果你们都需要打上错误信息,那么,投下者就会:

class Derived : public Base
{
    Derived() {
        registerPrepFn( static_cast<PrepFn>(&Derived::derivedPrepFn) );
    };

    void derivedPrepFn( int n ) {};

}

()

见http://ideone.com/BB9oy>。 例如,工作实例。

不允许这样做。 行为转换是通过在物体制造时间对物体进行多变化来实现的。

如果您需要目标形成后的行为转变,你可能会将动态行为重新归入另一组多变的类别,并担任“点人”的类别,而其行为是正确的。 请谷歌“精选类别”软件模式。





相关问题
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?