我试图在c ++上使用属性来代替太多的setter,而getter函数在数据类中有很多成员变量。
有两个属性类。第一个具有固定的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
};
并且我在将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();
}
};
但是在使用RWProperty时,即使我传递了模板的4个参数,比如成员类型,类类型有setter和getter函数,getter函数指针,setter函数指针按顺序,它说
&#34;模板参数数量错误(3,应为4): RWProperty&lt; uint,OptionSet,&amp; getNumberOfVbo,&amp; setNumberOfVbo&gt; uNumberOfVbo&#34;
&#34;提供&#39;模板&lt; class T,类Object, T(Object :: * real_getter)(),T(Object :: * real_setter)(const T&amp;)&gt;类 RWProperty:类RWProperty&#34;
我想我在模板中传递参数时做错了。
有谁知道发生了什么事?
答案 0 :(得分:0)
您的代码中有三个错误:
首先,要获取成员函数的地址,您需要包含类名:
public String executeScalarCommandString(String conString, OdbcCommand command)
{
//starting stopwatch here
String result = "";
// remember we have already a connection which already open
// so we dont have to create new connection
/* we dont need this :: using (OdbcConnection connection = new OdbcConnection(conString))*/
{
try
{
// i'll assume the connection already on command object
//command.Connection = connection;
command.CommandType = CommandType.Text;
//connection.Open();
object obj = command.ExecuteScalar();
if (obj != null && DBNull.Value != obj)
result = obj.ToString();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
//stop and restart stopwatch here. Avg elapsed time: 10 ms
}
//stop stopwatch here. Avg elapsed time: 76 ms
return result;
}
其次,要形成指向RWProperty<uint, OptionSet
, &OptionSet::getNumberOfVbo
// ~~~~~~~~~~~^
, &OptionSet::setNumberOfVbo> uNumberOfVbo;
// ~~~~~~~~~~~^
限定成员函数的指针,需要在该指针的声明中附加const
关键字:
const
最后,T (Object::*real_getter)() const
// ~~~~^ to match 'uint getNumberOfVbo() const'
内的OptionSet
本身是不完整的类型。除非首先声明该成员的声明,否则您不能引用其成员。这基本上意味着您需要在OptionSet
内对声明进行重新排序,以便{/ 1}}和OptionSet
来之前声明setNumberOfVbo
数据成员:
getNumberOfVbo