成员函数指针作为构造函数参数

Posted

技术标签:

【中文标题】成员函数指针作为构造函数参数【英文标题】:Member function pointer as constructor parameter 【发布时间】:2014-01-03 22:22:57 【问题描述】:

我有一个属性类,用于在类中设置 getter 和 setter 属性。它可以工作,但要使用它,我必须使用公共变量设置属性,并在构造函数中调用 3 个方法,1 个函数设置类实例,1 个设置 setter,1 个设置 getter。

class PropTest

private:
    int m_nCount;

    int getCount()
    
        return m_nCount;
    
    void setCount(int nCount)
    
        m_nCount = nCount;
    

public:
    PropTest()
    
        Count.setObject(this);
        Count.setSetter(&PropTest::setCount);
        Count.setGetter(&PropTest::getCount);
    

    Property<PropTest, int> Count = Property<PropTest, int>(PropertyPermission::READ | PropertyPermission::WRITE);
;

这很好,但我试图把它全部归结为一行......

Property<PropTest, int> Count = Property<PropTest, int>(this, &PropTest::getCount, &PropTest::setCount, PropertyPermission::READ | PropertyPermission::WRITE);

但是当我将新的构造函数添加到类并尝试编译时,我得到: error C2276: '&' : 对绑定成员函数表达式的非法操作

新的构造函数是:

template <class Class, typename Return> !!This template is for the whole class not the function. Just figured you needed to see it.

Property(Class* pObject, Return(Class::*pGetter)(), void (Class::*pSetter)(Return pValue), const int pPropertyPermission = PropertyPermission::READ | PropertyPermission::WRITE)

添加实例立即生效,但 getter 和 setter 参数是导致错误的原因。但是在 setGetter 和 setSetter 方法中使用相同的设置时可以编译并正常工作。

谢谢...

编辑

Property<PropTest, int> Count this, &PropTest::getCount, &PropTest::setCount, PropertyPermission::READ | PropertyPermission::WRITE ;

是导致错误的行吗...

属性类-

static const enum PropertyPermission

    READ = (1 << 0),
    WRITE = (1 << 1)
;

template <class Class, typename Return>
class Property

private:
    Class* _object;
    Return(Class::*_getter)();
    void (Class::*_setter)(Return value);

public:
    Property(const int pPropertyPermission = PropertyPermission::READ | PropertyPermission::WRITE)
    
        _getter = nullptr;
        _object = nullptr;
        _setter = nullptr;
        permission = pPropertyPermission;
    
    Property(Class* pObject, const int pPropertyPermission = PropertyPermission::READ | PropertyPermission::WRITE)
    
        _getter = nullptr;
        _object = pObject;
        _setter = nullptr;
        permission = pPropertyPermission;
    
    Property(Class* pObject, Return(Class::*pGetter)(), void (Class::*pSetter)(Return pValue), const int pPropertyPermission = PropertyPermission::READ | PropertyPermission::WRITE)
    
        _getter = nullptr;
        _object = pObject;
        _setter = nullptr;
        permission = pPropertyPermission;
    

    operator Return()
    
        //test for object and getter != null
        if (!(permission & PropertyPermission::READ))
    
        //throw write only
    
    return (_object->*_getter)();
    

    Return operator =(const Return& pValue)
    
        //test for object and setter != null
        if (!(permission & PropertyPermission::WRITE))
        
            //throw read only
        
        (_object->*_setter)(pValue);
        return pValue;
    

    void setGetter(Return(Class::*pGetter)())
    
        //test for object != null
        _getter = pGetter;
    
    void setObject(Class* pObject)
    
        _object = pObject;
    
    void setSetter(void (Class::*pSetter)(Return pValue))
    
        //test for object != null
        _setter = pSetter;
    

    int permission;
;

测试-

int main(int pArgumentLength, char* pArguments[])

    int i = 5;
    int j = 0;
    PropTest* test = new PropTest();
    test->Count = i;
    j = test->Count;
    std::cout << test->Count << std::endl;
    std::cout << j << std::endl;
    delete test;

编辑

我使用视觉工作室... 当我关闭 - 将警告视为错误时 - 它编译但有一个异常:

Unhandled exception at 0x7445C9F5 in TestBed.exe: 0xC0000005: Access violation executing location 0x00000000.

有 2 个警告:

Warning 2   warning C4100: 'pSetter' : unreferenced formal parameter ...\testbed\program.cpp    58  1   TestBed

Warning 3   warning C4100: 'pGetter' : unreferenced formal parameter    ...\testbed\program.cpp 58  1   TestBed

第 58 行是构造函数:

Property(Class* pObject, Return(Class::*pGetter)(), void (Class::*pSetter)(Return pValue), const int pPropertyPermission = PropertyPermission::READ | PropertyPermission::WRITE)

解决方案

所以这行导致了错误:

Property<PropTest, int> Count = Property<PropTest, int>(this, &PropTest::getCount, &PropTest::setCount, PropertyPermission::READ | PropertyPermission::WRITE);

使用它来编译并运行良好:(Kerrek SB)

Property<PropTest, int> Count this, &PropTest::getCount, &PropTest::setCount, PropertyPermission::READ | PropertyPermission::WRITE ;

但我不知道为什么......

感谢所有回复。

【问题讨论】:

你在哪一行得到错误? 顺便写Property&lt;PropTest, int&gt; Count PropertyPermission::READ | PropertyPermission::WRITE ;之类的就行了 该行是设置: Property Count = Property(this, &PropTest::getCount, &PropTest::setCount, PropertyPermission::READ | PropertyPermission::WRITE ); 你能做一个独立的、完整的例子吗?看起来这应该是正确的;您的错误可能在其他地方。 Kerrek SB,非常感谢 【参考方案1】:

在您的 Property 类实现中,您永远不会设置指针成员值:

Property(Class* pObject, Return(Class::*pGetter)(), void (Class::*pSetter)(Return pValue), const int pPropertyPermission = PropertyPermission::READ | PropertyPermission::WRITE)

    _getter = nullptr;
    _object = pObject;
    _setter = nullptr;
    permission = pPropertyPermission;

应改为:

Property(Class* pObject, Return(Class::*pGetter)(), void (Class::*pSetter)(Return pValue), const int pPropertyPermission = PropertyPermission::READ | PropertyPermission::WRITE)

    _getter = pGetter;
    _object = pObject;
    _setter = pSetter;
    permission = pPropertyPermission;

【讨论】:

以上是关于成员函数指针作为构造函数参数的主要内容,如果未能解决你的问题,请参考以下文章

C ++:将成员函数作为普通函数指针传递的闭包

泛型成员函数指针作为模板参数

C++ 类成员函数地址

C++ CreateThread函数如何传递this指针作为参数

07. this指针,构造和析构,new和delete

07. this指针,构造和析构,new和delete