Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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++风格的属性。它基本上是一个围绕typename T的包装类,具有用户定义的get/set函数: #include <functional> template <typename T> struct property { property(std::function<T(void)> getter, std::function<void(const T&)> setter) : get(getter), set(setter) { } property(std::function<T(void)> _getter) : get(_getter), set([](const T&) { throw std::exception(); }) { } property<T>& operator=(const T& value) { set(value); return *this; } // w i h o s h r ? T operator=(const T& value) { set(value); return get(); } // h c t u e e e operator T() { return get(); } property<T>& operator=(property<T>& value) { set(value); return *this; }; //... T operator=(property<T>& value) { set(value); return get(); }; //... property<T>& operator=(property<T>&) = delete; // arithmetic / assignment #define OP(__OP__) \ T operator __OP__(const T& rhs) { return get() __OP__ rhs; } \ T operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return get(); } //... //property<T>& operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return *this; } //... OP(+); OP(-); OP(*); OP(/); OP(%); OP(^); OP(&); OP(|); #undef OP // bit shift #define OP(__OP__) \ T operator __OP__(int rhs) { return get() __OP__ rhs; } \ property<T>& operator __OP__##= (int rhs) { set(get() __OP__ rhs); return *this; } OP(<<); OP(>>); #undef OP // comparison / logic #define OP(__OP__) bool operator __OP__(const T& rhs) { return get() __OP__ rhs;} OP(<); OP(>); OP(==); OP(!=); OP(<=); OP(>=); OP(&&); OP(||); #undef OP // inc/dec #define OP(__OP__) \ property<T>& operator __OP__##__OP__() { set(get() __OP__ 1); return *this; } \ T operator __OP__##__OP__(int) { T value = get(); operator __OP__##__OP__(); return value; } OP(+); OP(-); #undef OP T operator ~() { return ~get(); } bool operator !() { return !get(); } private: std::function<T(void)> get; std::function<void(const T&)> set; }; struct test { property<int> a = property<int>([&]() { return x/10; }, [&](int value) { x = value*10; }); property<int> b = property<int>([&]() { return y+10; }, [&](int value) { y = value-10; }); private: int x, y; }; using namespace std; void main() { test x; x.a = 5; x.a = x.a + 15; x.a *= 5; x.b = x.a; x.b += x.a / 10; cout << "property a=" << dec << x.a << endl; // should be 100 cout << "property b=" << dec << x.b << endl; // should be 110 } #包括 模板 结构属性 { 属性(std::函数getter、std::函数setter) :get(getter),set(setter){ 属性(std::function\u getter) :get(_getter),set([](const T&){throw std::exception();}){} 属性和运算符=(常量T和值){set(值);返回*this;}//w i h s r? T运算符=(常量T&value){set(value);返回get();}//hctue 运算符T(){return get();} 属性和运算符=(属性和值){set(值);返回*this;};//。。。 T运算符=(属性和值){set(值);返回get();};//。。。 属性和运算符=(属性和)=删除; //算术/赋值 #定义OP(OP)\ T运算符uuuu OP_uuuu(const T&rhs){return get()uuuu OP_uuuuuh;}\ T运算符__###=(常量T&rhs){set(get()__;OP_#rhs);返回get()}/。。。 //属性和运算符uuuuuuuuuuu###=(常量T和rhs){set(get()uuuuuuuuuuuuuuuurhs);返回*this;}/。。。 OP(+);OP(-);OP(*);OP(/);OP(%);OP(^);OP(&);OP(|); #未定义操作 //位移位 #定义OP(OP)\ T运算符uuu OP_uuu(int-rhs){return get()\ 属性和运算符uuuuuuuuuuuu##=(int-rhs){set(get()uuuu-OP\uuuurhs);返回*this;} OP(); #未定义操作 //比较/逻辑 #定义OP(uuu OP_uuu)bool运算符uu OP_uuu(const T&rhs){return get()uuu OP_uuu_uh;} OP(;OP(=);OP(!=);OP(=);OP(&&&);OP(| |); #未定义操作 //公司/十二月 #定义OP(OP)\ 属性和运算符uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\ T运算符{T value=get();运算符} OP(+);OP(-); #未定义操作 T运算符~(){return~get();} 布尔运算符!(){return!get();} 私人: std::函数get; std::函数集; }; 结构测试 { 属性a=属性([&]() { 返回x/10; }, [&](整数值) { x=值*10; }); 属性b=属性([&]() { 返回y+10; }, [&](整数值) { y=值-10; }); 私人: int x,y; }; 使用名称空间std; void main() { 测试x; x、 a=5; x、 a=x.a+15; x、 a*=5; x、 b=x.a; x、 b+=x.a/10; 在大多数情况下,用复本复出都可以,但有些反常的情况是不会的。 struct A { A(int i) : i_{i} {} int i_; A operator=(A const& A) { i_ = a.i_; return *this; } };_C++_Oop_Properties - Fatal编程技术网

包装类中赋值运算符的返回类型? 我提出了下面的代码来实现C++中的C++风格的属性。它基本上是一个围绕typename T的包装类,具有用户定义的get/set函数: #include <functional> template <typename T> struct property { property(std::function<T(void)> getter, std::function<void(const T&)> setter) : get(getter), set(setter) { } property(std::function<T(void)> _getter) : get(_getter), set([](const T&) { throw std::exception(); }) { } property<T>& operator=(const T& value) { set(value); return *this; } // w i h o s h r ? T operator=(const T& value) { set(value); return get(); } // h c t u e e e operator T() { return get(); } property<T>& operator=(property<T>& value) { set(value); return *this; }; //... T operator=(property<T>& value) { set(value); return get(); }; //... property<T>& operator=(property<T>&) = delete; // arithmetic / assignment #define OP(__OP__) \ T operator __OP__(const T& rhs) { return get() __OP__ rhs; } \ T operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return get(); } //... //property<T>& operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return *this; } //... OP(+); OP(-); OP(*); OP(/); OP(%); OP(^); OP(&); OP(|); #undef OP // bit shift #define OP(__OP__) \ T operator __OP__(int rhs) { return get() __OP__ rhs; } \ property<T>& operator __OP__##= (int rhs) { set(get() __OP__ rhs); return *this; } OP(<<); OP(>>); #undef OP // comparison / logic #define OP(__OP__) bool operator __OP__(const T& rhs) { return get() __OP__ rhs;} OP(<); OP(>); OP(==); OP(!=); OP(<=); OP(>=); OP(&&); OP(||); #undef OP // inc/dec #define OP(__OP__) \ property<T>& operator __OP__##__OP__() { set(get() __OP__ 1); return *this; } \ T operator __OP__##__OP__(int) { T value = get(); operator __OP__##__OP__(); return value; } OP(+); OP(-); #undef OP T operator ~() { return ~get(); } bool operator !() { return !get(); } private: std::function<T(void)> get; std::function<void(const T&)> set; }; struct test { property<int> a = property<int>([&]() { return x/10; }, [&](int value) { x = value*10; }); property<int> b = property<int>([&]() { return y+10; }, [&](int value) { y = value-10; }); private: int x, y; }; using namespace std; void main() { test x; x.a = 5; x.a = x.a + 15; x.a *= 5; x.b = x.a; x.b += x.a / 10; cout << "property a=" << dec << x.a << endl; // should be 100 cout << "property b=" << dec << x.b << endl; // should be 110 } #包括 模板 结构属性 { 属性(std::函数getter、std::函数setter) :get(getter),set(setter){ 属性(std::function\u getter) :get(_getter),set([](const T&){throw std::exception();}){} 属性和运算符=(常量T和值){set(值);返回*this;}//w i h s r? T运算符=(常量T&value){set(value);返回get();}//hctue 运算符T(){return get();} 属性和运算符=(属性和值){set(值);返回*this;};//。。。 T运算符=(属性和值){set(值);返回get();};//。。。 属性和运算符=(属性和)=删除; //算术/赋值 #定义OP(OP)\ T运算符uuuu OP_uuuu(const T&rhs){return get()uuuu OP_uuuuuh;}\ T运算符__###=(常量T&rhs){set(get()__;OP_#rhs);返回get()}/。。。 //属性和运算符uuuuuuuuuuu###=(常量T和rhs){set(get()uuuuuuuuuuuuuuuurhs);返回*this;}/。。。 OP(+);OP(-);OP(*);OP(/);OP(%);OP(^);OP(&);OP(|); #未定义操作 //位移位 #定义OP(OP)\ T运算符uuu OP_uuu(int-rhs){return get()\ 属性和运算符uuuuuuuuuuuu##=(int-rhs){set(get()uuuu-OP\uuuurhs);返回*this;} OP(); #未定义操作 //比较/逻辑 #定义OP(uuu OP_uuu)bool运算符uu OP_uuu(const T&rhs){return get()uuu OP_uuu_uh;} OP(;OP(=);OP(!=);OP(=);OP(&&&);OP(| |); #未定义操作 //公司/十二月 #定义OP(OP)\ 属性和运算符uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\ T运算符{T value=get();运算符} OP(+);OP(-); #未定义操作 T运算符~(){return~get();} 布尔运算符!(){return!get();} 私人: std::函数get; std::函数集; }; 结构测试 { 属性a=属性([&]() { 返回x/10; }, [&](整数值) { x=值*10; }); 属性b=属性([&]() { 返回y+10; }, [&](整数值) { y=值-10; }); 私人: int x,y; }; 使用名称空间std; void main() { 测试x; x、 a=5; x、 a=x.a+15; x、 a*=5; x、 b=x.a; x、 b+=x.a/10; 在大多数情况下,用复本复出都可以,但有些反常的情况是不会的。 struct A { A(int i) : i_{i} {} int i_; A operator=(A const& A) { i_ = a.i_; return *this; } };

包装类中赋值运算符的返回类型? 我提出了下面的代码来实现C++中的C++风格的属性。它基本上是一个围绕typename T的包装类,具有用户定义的get/set函数: #include <functional> template <typename T> struct property { property(std::function<T(void)> getter, std::function<void(const T&)> setter) : get(getter), set(setter) { } property(std::function<T(void)> _getter) : get(_getter), set([](const T&) { throw std::exception(); }) { } property<T>& operator=(const T& value) { set(value); return *this; } // w i h o s h r ? T operator=(const T& value) { set(value); return get(); } // h c t u e e e operator T() { return get(); } property<T>& operator=(property<T>& value) { set(value); return *this; }; //... T operator=(property<T>& value) { set(value); return get(); }; //... property<T>& operator=(property<T>&) = delete; // arithmetic / assignment #define OP(__OP__) \ T operator __OP__(const T& rhs) { return get() __OP__ rhs; } \ T operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return get(); } //... //property<T>& operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return *this; } //... OP(+); OP(-); OP(*); OP(/); OP(%); OP(^); OP(&); OP(|); #undef OP // bit shift #define OP(__OP__) \ T operator __OP__(int rhs) { return get() __OP__ rhs; } \ property<T>& operator __OP__##= (int rhs) { set(get() __OP__ rhs); return *this; } OP(<<); OP(>>); #undef OP // comparison / logic #define OP(__OP__) bool operator __OP__(const T& rhs) { return get() __OP__ rhs;} OP(<); OP(>); OP(==); OP(!=); OP(<=); OP(>=); OP(&&); OP(||); #undef OP // inc/dec #define OP(__OP__) \ property<T>& operator __OP__##__OP__() { set(get() __OP__ 1); return *this; } \ T operator __OP__##__OP__(int) { T value = get(); operator __OP__##__OP__(); return value; } OP(+); OP(-); #undef OP T operator ~() { return ~get(); } bool operator !() { return !get(); } private: std::function<T(void)> get; std::function<void(const T&)> set; }; struct test { property<int> a = property<int>([&]() { return x/10; }, [&](int value) { x = value*10; }); property<int> b = property<int>([&]() { return y+10; }, [&](int value) { y = value-10; }); private: int x, y; }; using namespace std; void main() { test x; x.a = 5; x.a = x.a + 15; x.a *= 5; x.b = x.a; x.b += x.a / 10; cout << "property a=" << dec << x.a << endl; // should be 100 cout << "property b=" << dec << x.b << endl; // should be 110 } #包括 模板 结构属性 { 属性(std::函数getter、std::函数setter) :get(getter),set(setter){ 属性(std::function\u getter) :get(_getter),set([](const T&){throw std::exception();}){} 属性和运算符=(常量T和值){set(值);返回*this;}//w i h s r? T运算符=(常量T&value){set(value);返回get();}//hctue 运算符T(){return get();} 属性和运算符=(属性和值){set(值);返回*this;};//。。。 T运算符=(属性和值){set(值);返回get();};//。。。 属性和运算符=(属性和)=删除; //算术/赋值 #定义OP(OP)\ T运算符uuuu OP_uuuu(const T&rhs){return get()uuuu OP_uuuuuh;}\ T运算符__###=(常量T&rhs){set(get()__;OP_#rhs);返回get()}/。。。 //属性和运算符uuuuuuuuuuu###=(常量T和rhs){set(get()uuuuuuuuuuuuuuuurhs);返回*this;}/。。。 OP(+);OP(-);OP(*);OP(/);OP(%);OP(^);OP(&);OP(|); #未定义操作 //位移位 #定义OP(OP)\ T运算符uuu OP_uuu(int-rhs){return get()\ 属性和运算符uuuuuuuuuuuu##=(int-rhs){set(get()uuuu-OP\uuuurhs);返回*this;} OP(); #未定义操作 //比较/逻辑 #定义OP(uuu OP_uuu)bool运算符uu OP_uuu(const T&rhs){return get()uuu OP_uuu_uh;} OP(;OP(=);OP(!=);OP(=);OP(&&&);OP(| |); #未定义操作 //公司/十二月 #定义OP(OP)\ 属性和运算符uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\ T运算符{T value=get();运算符} OP(+);OP(-); #未定义操作 T运算符~(){return~get();} 布尔运算符!(){return!get();} 私人: std::函数get; std::函数集; }; 结构测试 { 属性a=属性([&]() { 返回x/10; }, [&](整数值) { x=值*10; }); 属性b=属性([&]() { 返回y+10; }, [&](整数值) { y=值-10; }); 私人: int x,y; }; 使用名称空间std; void main() { 测试x; x、 a=5; x、 a=x.a+15; x、 a*=5; x、 b=x.a; x、 b+=x.a/10; 在大多数情况下,用复本复出都可以,但有些反常的情况是不会的。 struct A { A(int i) : i_{i} {} int i_; A operator=(A const& A) { i_ = a.i_; return *this; } };,c++,oop,properties,C++,Oop,Properties,然后试试这个: A a1{1}; A a2{2}; A a3{3}; (a3 = a2) = a1; // What is the value of a3::i_? 通过复制返回意味着a3=a2赋值生成一个临时变量,该变量被重新分配为等于a1。换句话说,赋值后a3::i_==2。如果您通过引用返回,a3::i_==1您不应该在?@m.s.发布此消息吗?谢谢,我不知道codereview网站的情况我应该在那里转载,还是有办法把问题转移过去?

然后试试这个:

A a1{1};
A a2{2};
A a3{3};
(a3 = a2) = a1; // What is the value of a3::i_?

通过复制返回意味着
a3=a2
赋值生成一个临时变量,该变量被重新分配为等于
a1
。换句话说,赋值后
a3::i_==2
。如果您通过引用返回,
a3::i_==1

您不应该在?@m.s.发布此消息吗?谢谢,我不知道codereview网站的情况我应该在那里转载,还是有办法把问题转移过去?