[英]Member function pointer as constructor parameter
我有一個屬性類,我用它來設置類中的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:'&':對綁定成員函數表達式的非法操作
新的構造函數是:
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類實現中,您永遠不會設置指針成員值:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.