我試圖使用C++的屬性來使用它,而不是太多的setter和數據類中的getter函數有很多成員變量。錯誤的模板參數數量(3,應該是4)
有兩個屬性類。第一個具有固定的setter和getter函數默認設置,get。第二個支持使用自定義setter和類的getter函數。下面是代碼
template <class T>
class Property
{
T data;
public:
// access with function call syntax
Property() : data() { }
T operator()() const
{
return data;
}
T operator()(T const & value)
{
data = value;
return data;
}
// access with get()/set() syntax
T get() const
{
return data;
}
T set(T const & value)
{
data = value;
return data;
}
// access with '=' sign
operator T() const
{
return data;
}
T operator = (T const & value)
{
data = value;
return data;
}
typedef T value_type; // might be useful for template deductions
};
// a read-write property which invokes user-defined functions
template <class T, class Object, T(Object::*real_getter)(), T(Object::*real_setter)(T const &) >
class RWProperty
{
Object * my_object;
public:
// this function must be called by the containing class, normally in a
// constructor, to initialize the RWProperty so it knows where its
// real implementation code can be found
void operator() (Object * obj)
{
my_object = obj;
}
// function call syntax
T operator()() const
{
return (my_object->*real_getter)();
}
T operator()(T const & value)
{
return (my_object->*real_setter)(value);
}
// get/set syntax
T get() const
{
return (my_object->*real_getter)();
}
T set(T const & value)
{
return (my_object->*real_setter)(value);
}
// access with '=' sign
operator T() const
{
return (my_object->*real_getter)();
}
T operator = (T const & value)
{
return (my_object->*real_setter)(value);
}
typedef T value_type; // might be useful for template deductions
};
,並把它納入項目代碼
#include <QString>
class OptionSet
{
public:
explicit OptionSet() {}
Property<QString> m_MeshMode;
RWProperty<uint, OptionSet, &getNumberOfVbo, &setNumberOfVbo> uNumberOfVbo;
// this causes problems
protected:
private:
Property<uint> m_uNumberOfVbo;
uint setNumberOfVbo(const uint& rVboCount)
{
// something to do here
return m_uNumberOfVbo(rVboCount);
}
uint getNumberOfVbo() const
{
return m_uNumberOfVbo();
}
};
但在使用前RWProperty我測試在OptionSet類這些特性,即使我通過了4個參數模板狀的部件類型,類類型有setter和getter函數,getter函數指針,二傳手函數指針,以,它說
「的模板參數錯號(3,應該是4): RWProperty < UINT,OptionSet,& getNumberOfVbo,& setNumberOfVbo> uNumberOfVbo 「
」 的「模板<類T,類對象, T(對象:: * real_getter)(),T(對象:: * real_setter) (常量牛逼&)>類 RWProperty:類RWProperty」
我想我做錯了什麼來傳遞參數模板。
有沒有人知道發生了什麼事?
能否請您編輯您的問題,包括實際的,完整的,未編輯的錯誤日誌? –
1.要獲得成員函數的地址,需要包含類名「&OptionSet :: getNumberOfVbo」,2.在你想獲得成員函數的地址處,「OptionSet」不完整 –
@JoachimPileborg: m很抱歉,我編輯了它 – zzangzzangmen