Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何制作一个;计算属性";对于C++;使用模板初始化_C++_Templates - Fatal编程技术网

C++ 如何制作一个;计算属性";对于C++;使用模板初始化

C++ 如何制作一个;计算属性";对于C++;使用模板初始化,c++,templates,C++,Templates,我试图实现一个泛型方法,将一个计算值作为只读memver值放入一个类中 我使用以下宏成功地完成了它: #define READONLY_PROPERTY(datatype, containerclass, access, name)\ class name ## _ ## datatype ## _ROP {\ public:\ name ## _ ## datatype ## _ROP(containerclass &c_): cclass(c_) {}\

我试图实现一个泛型方法,将一个计算值作为只读memver值放入一个类中

我使用以下宏成功地完成了它:

#define READONLY_PROPERTY(datatype, containerclass, access, name)\
    class name ## _ ## datatype ## _ROP {\
public:\
    name ## _ ## datatype ## _ROP(containerclass &c_): cclass(c_) {}\
        operator datatype() const {return cclass.access();}\
private:\
    containerclass &cclass;\
}name;\
friend class name ## _ ## datatype ## _ROP
在本课程中使用的:

class TestClass {
public:
    TestClass(): x(0), y(0), pixels(*this) {}
    TestClass(int x_, int y_): x(x_), y(y_), pixels(*this) {}
    int x;
    int y;
    READONLY_PROPERTY(int, TestClass, getPix, pixels);
private:
    int getPix() {return x * y;}
};
生成以下工作代码(使用g++):

关键是我可以这样使用这个类:

TestClass tc(10,10);
std::cout << tc.pixels << std::endl;
TestClass-tc(10,10);
std::cout,因为getPix()在用作模板参数时未被声明,
\u instance
必须是
const
,因为ReadOnlyProperty构造函数参数是
const

template< class T, class U, U (T::*F)() const >
class ReadOnlyProperty {
public:
    ReadOnlyProperty(T const& instance): _instance(instance) {}
    operator U const &() const {return (_instance.*F)();}
private:
    const T& _instance;
};

class TestClass {
public:
    TestClass(): x(0), y(0), pixels(*this) {}
    TestClass(int x_, int y_): x(x_), y(y_), pixels(*this) {}
    int x;
    int y;
private:
    int getPix() const {return x * y;}
public:
    ReadOnlyProperty<TestClass, int, &TestClass::getPix> pixels;
};
模板
类只读属性{
公众:
只读属性(T常量和实例):\u实例(实例){
运算符U const&()const{return(_instance.*F)(;}
私人:
const T&_实例;
};
类TestClass{
公众:
TestClass():x(0),y(0),像素(*this){
TestClass(intx_uu,inty_u):x(x_u),y(y_u),像素(*this){
int x;
int-y;
私人:
int getPix()常量{return x*y;}
公众:
只读属性像素;
};
编辑:多亏了Georg Fritzsche,根据他的评论,最后一个模板参数应该是一个
const
成员函数和
\u实例。*F()
需要括号:)(哦!我忘了!)

这个怎么样:

#include <functional>

template<class R, class UA, class P>
class MyBind
{
    public:
        MyBind(UA unaryAction, P param)
            :_unaryAction(unaryAction)
            ,_parameter(param)
        {}
        operator R const&() const
        {
            return _action(_parameter);
        }
    private:
        UA  _unaryAction;
        P&  _parameter;
};
#包括
模板
类MyBind
{
公众:
MyBind(UA-unaryAction,P参数)
:_unaryAction(unaryAction)
,_参数(参数)
{}
运算符R常量和(()常量
{
返回_动作(_参数);
}
私人:
行动;
P&_参数;
};
然后可以这样使用:

class TestClass
{
        typedef std::const_mem_fun_t<int, TestClass> MethodCall;
        int getPix() const
        {
            return x * y;
        }
    public:
        TestClass(int x_, int y_)
            :x(x_)
            ,y(y_)
            ,pixels(std::mem_fun(&TestClass::getPix), *this)
        {}
        int     x;
        int     y;
        MyBind<int, MethodCall, TestClass>     pixels;
};
class测试类
{
typedef std::const_mem_fun_t MethodCall;
int getPix()常量
{
返回x*y;
}
公众:
测试类(int x_u2;,int y_2;)
:x(x_)
,y(y_)
,像素(std::mem_-fun(&TestClass::getPix),*这个)
{}
int x;
int-y;
MyBind像素;
};

如果一个简单的成员函数也能完成这项工作,为什么还要经历所有这些麻烦呢<代码> int TestClass::Pixels:(const {返回x*y;},然后<代码> STD::CUT,因为我只是想学习一些关于C++模板的主题,我喜欢如果计算或存储属性(在类使用的观点中)隐藏的习惯用法。这可能是因为我现在是一个比C++更为Python程序员的人,我习惯了PyHton属性概念。C++中的属性通常是通过成员函数(即访问器)访问的,对于那些只携带数据的“小”结构的成员(例如:代码>结构点{int x,y;};< /C>)有一些例外。。如果它在
const
实例上调用它,则应该使用
U(T::*F)()const
成员函数。它也应该是
(_instance.*F)(
)-函数调用
()
的优先级高于
*
@Georg Fritzsche:是的,它应该是const,我刚刚清除了它的错误:)谢谢你的注意,我将最后一个模板参数改为使用
const
成员函数。谢谢你们。答案和评论已经解决了我的问题。
#include <functional>

template<class R, class UA, class P>
class MyBind
{
    public:
        MyBind(UA unaryAction, P param)
            :_unaryAction(unaryAction)
            ,_parameter(param)
        {}
        operator R const&() const
        {
            return _action(_parameter);
        }
    private:
        UA  _unaryAction;
        P&  _parameter;
};
class TestClass
{
        typedef std::const_mem_fun_t<int, TestClass> MethodCall;
        int getPix() const
        {
            return x * y;
        }
    public:
        TestClass(int x_, int y_)
            :x(x_)
            ,y(y_)
            ,pixels(std::mem_fun(&TestClass::getPix), *this)
        {}
        int     x;
        int     y;
        MyBind<int, MethodCall, TestClass>     pixels;
};