本文介绍了超载“,”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我编写了以下代码,但它没有返回我的期望,为什么? #include< iostream> using namespace std; class Other { public: int i; 其他(int x = 1) { i = x; } 其他* operator-(){return this;} 其他&运算符+(其他t) { i + = ti; 返回* this; } 其他&操作员,(其他) { i = oth.i; 返回* this; } }; int main() { 其他o0,o1,o2(4),o3(5); o0-> i = 100; cout< < o0.i<< " \\\" << o0-> i<< " \ n"; //这里它返回5而不是6为什么????????????????? /> 其他牛=(o1 + o1,o3 = o2 + o1); // ------------------ cout<< ox.i<< endl; 返回0; }Hi, I coded the following but It does not return what I expect, why?#include <iostream>using namespace std;class Other{public:int i;Other(int x=1){i = x;}Other *operator-() { return this;}Other &operator+ (Other t){i += t.i;return *this;}Other &operator,(Other oth){i = oth.i;return *this;}};int main(){Other o0, o1, o2(4), o3(5);o0->i = 100;cout << o0.i << "\n" << o0->i << "\n";// HERE it returns 5 AND not 6 WHY ???????????????????Other ox = (o1 + o1, o3 = o2 + o1);// ------------------cout << ox.i << endl;return 0;}推荐答案 因为你有5. 表达式评估: 其他牛=((o1 + o1) ,(o3 = o2 + o1)); 或带名字 其他ox = o1.operator +(o1).operator,(o3.operator = (o2.operato r +(o1))); 因为o3是5,所以o1也是5而牛是5. 原因是重载运算符,与 POD运算符没有相同的优先级。是非常令人困惑的。Because you have 5.The expression evaluates:Other ox = ( (o1 + o1) , (o3 = o2 + o1) );Or with namesOther ox = o1.operator+(o1).operator,(o3.operator=(o2.operato r+(o1)));Since o3 is 5, then o1 is also 5 and ox is 5.The reason is overloaded operator, doesn''t have the same precedence asPOD operator,. Is is very confusing. MichaelMichael 我怀疑这可能是未定义的行为因为+运算符具有相同的优先级,并且它们的评估顺序取决于 编译器。评估顺序是''+'',''='','',''基于优先级和 因此代码不安全。 JBI suspect that this could be undefined behaviour as the + operators haveequal precedence and the order they are evaluated depends on thecompiler. evaluation order is ''+'', ''='', '','' based on precedence andhence the code is not safe.JB 因为你有5. 表达式评估: 其他牛=((o1 + o1) ,(o3 = o2 + o1)); 或带名字 其他ox = o1.operator +(o1).operator,(o3.operator = (o2.operato r +(o1))); 因为o3是5,所以o1也是5而牛是5. 原因是重载运算符,与 POD运算符没有相同的优先级。是非常令人困惑的。Because you have 5.The expression evaluates:Other ox = ( (o1 + o1) , (o3 = o2 + o1) );Or with namesOther ox = o1.operator+(o1).operator,(o3.operator=(o2.operato r+(o1)));Since o3 is 5, then o1 is also 5 and ox is 5.The reason is overloaded operator, doesn''t have the same precedence asPOD operator,. Is is very confusing. 这不是唯一令人困惑的事情!你对operator,()的使用是如此的b 混淆了!为什么在地球上你想要做那样的事情? 你有什么可能的理由? 在旁注,为什么不是算子? ,()与常规 '',''运算符相同的优先级?我认为所有用户定义的运算符都具有与内置函数相同的优先级 。 Adrian - ================================================== ======== Adrian Hawryluk BSc。计算机科学 ---------------------------------------- ------------------ 专攻:UML中的OOD方法 C,C ++及更多的OOP方法 RT嵌入式编程 __--------------------------- -------------------__ ----- [博客: http://adrians-musings.blogspot.com/] ----- '' - -------------------------------------------------- ----'' 我的新闻组作品根据广告获得许可 Commons Attribution-Noncommercial-Share Alike 3.0 License http://creativecommons.org/licenses/by-nc-sa/3.0/ ========================================== ======== ========That is not the only thing confusing! Your use of operator,() is the soobfuscated! Why on Earth would you want to do something like that?What possible reason could you have?On a side note, why isn''t operator,() the same precedence as the regular'','' operator? I thought that all user defined operators had the sameprecedence as the builtins.Adrian--================================================== ========Adrian Hawryluk BSc. Computer Science----------------------------------------------------------Specialising in: OOD Methodologies in UMLOOP Methodologies in C, C++ and moreRT Embedded Programming__--------------------------------------------------__----- [blog: http://adrians-musings.blogspot.com/] -----''--------------------------------------------------------''My newsgroup writings are licensed under the CreativeCommons Attribution-Noncommercial-Share Alike 3.0 License http://creativecommons.org/licenses/by-nc-sa/3.0/================================================== ======== 这篇关于超载“,”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-08 17:33