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

时间:2014-01-03 22:22:58

标签: c++ properties setter getter member-function-pointers

我有一个属性类,用于在类中设置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);

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

新构造函数是:

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;
}

修改

我使用视觉工作室...... 当我将-Treat警告变为错误时 - 它已编译但有例外:

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 };

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

感谢所有回复。

1 个答案:

答案 0 :(得分:0)

在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;
}