I tried to use property on c++ to use it instead of too many setter and getter function in data class have lots of member variable.
我试图在c ++上使用属性来使用它,而不是在数据类中有太多的setter和getter函数有很多成员变量。
there are two property class. first one has fixed setter and getter function by default set, get. second one support using custom setter and getter function of its class. below is the code
有两个属性类。第一个具有固定的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
};
and i'm testing these properties in OptionSet class before putting it into project code
我在将OptionSet类放入项目代码之前测试这些属性
#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();
}
};
but in use RWProperty, even i passed 4 arguments of template like member type, class type has setter and getter function, getter function pointer, setter function pointer in order, it says
但是在使用RWProperty时,即使我传递了模板的4个参数,如成员类型,类类型有setter和getter函数,getter函数指针,setter函数指针依次,它说
"wrong number of template arguments (3, should be 4) : RWProperty <uint, OptionSet, &getNumberOfVbo, &setNumberOfVbo> uNumberOfVbo"
“错误的模板参数数量(3,应为4):RWProperty
uNumberOfVbo” ,optionset,&getnumberofvbo,&setnumberofvbo>"provided for 'template<class T, class Object, T(Object::*real_getter)(), T(Object::*real_setter)(const T&)> class RWProperty : class RWProperty"
“提供'模板
类RWProperty:类RWProperty”
I guess i'm doing something wrong to pass arguments in template.
我想我在模板中传递参数做错了。
is there anyone knows what happened?
有谁知道发生了什么?
1 个解决方案
#1
There are three mistakes in your code:
您的代码中有三个错误:
Firstly, to get the address of a member function, you need to include the class name:
首先,要获取成员函数的地址,您需要包含类名:
RWProperty<uint, OptionSet
, &OptionSet::getNumberOfVbo
// ~~~~~~~~~~~^
, &OptionSet::setNumberOfVbo> uNumberOfVbo;
// ~~~~~~~~~~~^
Secondly, to form a pointer to const
qualified member function, you need to append const
keyword to the declaration of that pointer:
其次,要形成一个指向const限定成员函数的指针,需要将const关键字附加到该指针的声明:
T (Object::*real_getter)() const
// ~~~~^ to match 'uint getNumberOfVbo() const'
Lastly, OptionSet
inside OptionSet
itself is an incomplete type. You can't refer to its member unless the point of declaration of that member comes first. This basically means you need to reorder your declarations within OptionSet
so that setNumberOfVbo
and getNumberOfVbo
comes before you declare uNumberOfVbo
data member:
最后,OptionSet内部的OptionSet本身是一个不完整的类型。除非首先声明该成员的声明,否则您不能引用其成员。这基本上意味着您需要在OptionSet中重新排序声明,以便在声明uNumberOfVbo数据成员之前出现setNumberOfVbo和getNumberOfVbo:
class OptionSet
{
//...
uint setNumberOfVbo(const uint& rVboCount) { /*...*/ }
uint getNumberOfVbo() const { /*...*/ }
// Ok, now they are known and can be found in the class scope
//...
RWProperty<uint, OptionSet
, &OptionSet::getNumberOfVbo
, &OptionSet::setNumberOfVbo> uNumberOfVbo;
};
#1
There are three mistakes in your code:
您的代码中有三个错误:
Firstly, to get the address of a member function, you need to include the class name:
首先,要获取成员函数的地址,您需要包含类名:
RWProperty<uint, OptionSet
, &OptionSet::getNumberOfVbo
// ~~~~~~~~~~~^
, &OptionSet::setNumberOfVbo> uNumberOfVbo;
// ~~~~~~~~~~~^
Secondly, to form a pointer to const
qualified member function, you need to append const
keyword to the declaration of that pointer:
其次,要形成一个指向const限定成员函数的指针,需要将const关键字附加到该指针的声明:
T (Object::*real_getter)() const
// ~~~~^ to match 'uint getNumberOfVbo() const'
Lastly, OptionSet
inside OptionSet
itself is an incomplete type. You can't refer to its member unless the point of declaration of that member comes first. This basically means you need to reorder your declarations within OptionSet
so that setNumberOfVbo
and getNumberOfVbo
comes before you declare uNumberOfVbo
data member:
最后,OptionSet内部的OptionSet本身是一个不完整的类型。除非首先声明该成员的声明,否则您不能引用其成员。这基本上意味着您需要在OptionSet中重新排序声明,以便在声明uNumberOfVbo数据成员之前出现setNumberOfVbo和getNumberOfVbo:
class OptionSet
{
//...
uint setNumberOfVbo(const uint& rVboCount) { /*...*/ }
uint getNumberOfVbo() const { /*...*/ }
// Ok, now they are known and can be found in the class scope
//...
RWProperty<uint, OptionSet
, &OptionSet::getNumberOfVbo
, &OptionSet::setNumberOfVbo> uNumberOfVbo;
};