gpt4 book ai didi

c++ - 成员函数指针作为构造函数参数

转载 作者:太空狗 更新时间:2023-10-29 23:17:00 27 4
gpt4 key购买 nike

我有一个属性类,用于在类中设置 getter 和 setter 属性。它有效,但要使用它,我必须使用公共(public)变量设置属性,并在构造函数中调用 3 个方法,1 个函数用于设置类实例,1 个用于设置 setter,一个用于设置 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: '&' : 对绑定(bind)成员函数表达式的非法操作

新的构造函数是:

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

编辑

我使用 Visual Studio ...当我打开 -Treat warnings as errors- 它编译但有一个异常:

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 类实现中,您永远不会设置您的指针成员值:

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++ - 成员函数指针作为构造函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20913808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com