//派生类中的operator + 派生运算符+(const派生& rhs)const { 派生的tmp ; base * pbase; // !!! ****对我来说这是最好的方式吗?**** !!! // pbase =& tmp; * pbase = base :: operator +(rhs); tmp.dataC = this-> dataC + rhs.dataC; 返回tmp; } //打印值 void getValues() { cout<< dataA<<" "<< dataB<<" "<< dataC<<结束; } }; int main() { 派生一个(10,20,30); 派生b(1,2,3); 派生c; c = a + b; c.getValues(); } 输出符合预期:11 22 33 如果有人可以建议更好的方法来重载 派生类中的+运算符,如果我没有正确完成,我会感谢任何 回复。 -Thanks 戈达史密斯 [见 http://www.gotw.ca/resources/clcm.htm有关信息] [comp.lang.c ++。moderated。第一次海报:做到这一点! ] 解决方案 2004年7月21日星期三12:51:34 -0700,gorda写道: 有人能告诉我,如果我在派生类中正确地重载+运算符,或者有更好的方法吗? 我已经在审核小组上回复了这个问题。这是一个快速的 响应速度: - 请不要公开会员:) - 我推荐您实现operator +作为免费 函数thas是根据成员运算符+ =: 基本运算符+(基数lhs,基本const& rhs) ) { 返回lhs + = rhs; } 注意lhs通过因为据称提高了机会的价值 的优化:) - 对于派生,如果你去运算符+ =方式,那么它是''更简单: 派生& operator + =(派生const& other) { base :: operator + =(other); c_ + = other.c_; 返回*这个; } 你当然需要一个类似的算子+来源: 派生算子+(派生lhs,派生const& rhs) { 返回lhs + = rhs; } Ali 2004年7月21日星期三12:51:34 -0700,gorda写道: 我正在玩操作符重载和继承,专门重载基类中的+运算符及其派生类。 结构是simple:基类有两个int memebersdataA,dataB。派生类有一个额外的int成员dataC。我只是试图重载+运算符,以便''添加''两个对象将总结相应的int成员。 首先,继承可能不是这个 案例的最佳方法。至少对于下面给出的例子,如果 ''derived''包含''base''作为成员似乎更好。你需要问自己 是否''派生''真的''是'A'这个设计中的基础。 有人能告诉我,如果我在派生类中正确地重载了+运算符,或者是否有更好的方法来执行它? #include< iostream> 使用命名空间std; class base { public: // base int members int dataA,dataB; 请不要公开会员:) //默认构造函数 base():dataA(0),dataB(0) {} //重载构造函数 base(int x,int y):dataA(x),dataB(y){} //运算符+基类基本运算符+(const base& rhs)const { base tmp; tmp.dataA = this-> dataA + rhs.dataA; tmp。 dataB = this-> dataB + rhs.dataB; 返回tmp; 为什么默认构建然后分配给成员?这样更好: 返回基数(dataA + rhs.dataA, dataB + rhs.dataB); } 话虽如此,我建议您实现operator +作为免费的 函数thas是根据成员运算符+ =:实现的 基本运算符+(基数lhs,基数const和rhs) { 返回lhs + = rhs; } 请注意,lhs是按值传递的,据称可以提高优化的机会 优化:) }; 类派生:公共基础公共: // additonal派生类int成员 int dataC; [...] //运算符+派生类派生运算符+(const派生& rhs)const {派生tmp; base * pbase; // !!! ****对我来说这是最好的方法吗?**** !!! // pbase =& tmp; * pbase = base :: operator +(rhs); tmp.dataC = this-> dataC + rhs.dataC; 返回tmp; } 似乎没问题,但是如果你去操作员+ =方式,那么它就更简单了: 派生& operator + =(派生const& other) { base :: operator + =(other); c_ + = other.c_; 返回*这个; } 你当然需要一个类似的算子+来源: 派生算子+(派生lhs,派生const& rhs) { 返回lhs + = rhs; } 阿里 [见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息] [comp.lang.c ++。moderated。第一次海报:做到这一点! ] gorda写道:如果有人可以提出更好的方法来重载派生类中的+运算符没有正确完成,我会感激任何回复。 我建议你考虑实现operator + =而不是operator +。 完成后,按照operator + =实现operator +。 这是减少你遇到的一些复杂性的好方法, 如果你做得对,你的代码会更简洁,更易读。如果你已经完成了这个,请查看Boost.org的运营商库 < http://www.boost.org/libs/utility/ operators.htm>看看某些部分 如何更加通用化,以及如何实施 正确有效的操作员超载的进一步建议。 问候,Daniel - Daniel Frey aixigo AG - 金融解决方案&技术 Schlo?-Rahe-Stra?e 15,52072 Aachen,Germany fon:+49(0)241 936737-42,传真:+49(0) 241 936737-99 电子邮件: da ********* @ aixigo。 de ,web: http://www.aixigo.de 我们今天写的黑客成了明天的错误。 [见 http://www.gotw.ca/resources/clcm.htm 有关的信息] [comp.lang.c ++ .moderated。第一次海报:做到这一点! ] Hello,I am playing around with operator overloading and inheritence,specifically overloading the + operator in the base class and itsderived class.The structure is simple: the base class has two int memebers "dataA","dataB". The derived class has an additional int member "dataC". I amsimply trying to overload the + operator so that ''adding'' two objectswill sum up the corresponding int members.Can someone tell me if I''m overloading the + operator correctly in thederived class, or if there is a better way to do it?#include <iostream>using namespace std;class base{public://base int membersint dataA,dataB;//default constructorbase():dataA(0),dataB(0){}//overloaded constructorbase(int x, int y): dataA(x), dataB(y){}//operator + in the base classbase operator+(const base& rhs) const{base tmp;tmp.dataA = this->dataA + rhs.dataA;tmp.dataB = this->dataB + rhs.dataB;return tmp;}};class derived: public base{public://additonal derived class int memberint dataC;//default constructorderived():base(){dataC=0;}//overloaded constructorderived(int x, int y, int z):base(x,y){dataC=z;}//copy constructorderived(const derived& t): base(t){this->dataC = t.dataC;}//operator + in the derived classderived operator+(const derived &rhs) const{derived tmp;base *pbase;//!!!****is this the best way for my purpose?****!!!//pbase=&tmp;*pbase = base::operator+(rhs);tmp.dataC = this->dataC + rhs.dataC;return tmp;}//print valuesvoid getValues(){cout << dataA <<" "<< dataB <<" "<< dataC << endl;}};int main(){derived a(10,20,30);derived b(1,2,3);derived c;c = a + b;c.getValues();}The output is as expected: 11 22 33If someone can suggest a better way to overload the + operator in thederived class if I haven''t done it correctly, i''d appreciate anyresponses.-ThanksGorda Smith[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 解决方案 On Wed, 21 Jul 2004 12:51:34 -0700, gorda wrote: Can someone tell me if I''m overloading the + operator correctly in the derived class, or if there is a better way to do it?I already replied to this on the moderated group. Here is a quickresponse for speed:- No public members please :)- I recommend that you implement operator+ as a freefunction thas is implemented in terms of a member operator+=:base operator+ (base lhs, base const & rhs){return lhs += rhs;}Note that lhs is passed-by-value for allegedly improving the chancesof optimization :)- For derived, if you go the operator+= way, then it''s simpler:derived & operator+= (derived const & other){base::operator+=(other);c_ += other.c_;return *this;}You will need a similar operator+ for derived of course:derived operator+ (derived lhs, derived const & rhs){return lhs += rhs;}AliOn Wed, 21 Jul 2004 12:51:34 -0700, gorda wrote: I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that ''adding'' two objects will sum up the corresponding int members.First of all, inheritance may not be the best approach in thiscase. At least for the example given below, it seems to be better if''derived'' contains ''base'' as a member. You need to ask yourselfwhether ''derived'' really "IS A" ''base'' in this design. Can someone tell me if I''m overloading the + operator correctly in the derived class, or if there is a better way to do it? #include <iostream> using namespace std; class base { public: //base int members int dataA,dataB;No public members please :) //default constructor base():dataA(0),dataB(0) {} //overloaded constructor base(int x, int y): dataA(x), dataB(y) {} //operator + in the base class base operator+(const base& rhs) const { base tmp; tmp.dataA = this->dataA + rhs.dataA; tmp.dataB = this->dataB + rhs.dataB; return tmp;Why default construct and then assign to members? This is better:return base(dataA + rhs.dataA,dataB + rhs.dataB); }Having said this, I recommend that you implement operator+ as a freefunction thas is implemented in terms of a member operator+=:base operator+ (base lhs, base const & rhs){return lhs += rhs;}Note that lhs is passed-by-value for allegedly improving the chancesof optimization :) }; class derived: public base { public: //additonal derived class int member int dataC;[...] //operator + in the derived class derived operator+(const derived &rhs) const { derived tmp; base *pbase; //!!!****is this the best way for my purpose?****!!!// pbase=&tmp; *pbase = base::operator+(rhs); tmp.dataC = this->dataC + rhs.dataC; return tmp; }Seems to be ok, but if you go the operator+= way, then it''s simpler:derived & operator+= (derived const & other){base::operator+=(other);c_ += other.c_;return *this;}You will need a similar operator+ for derived of course:derived operator+ (derived lhs, derived const & rhs){return lhs += rhs;}Ali[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ]gorda wrote: If someone can suggest a better way to overload the + operator in the derived class if I haven''t done it correctly, i''d appreciate any responses.I suggest you think about implementing operator+= instead of operator+.After you have done that, implement operator+ in terms of operator+=.It''s a nice way of reducing some of the complexity you are experiencing,making your code ways more terse and readable if you do it right. If youhave finished this, have a look at Boost.org''s operators library<http://www.boost.org/libs/utility/operators.htm> to see how some partscan be even more generalised and for further advice on how to implementcorrect and efficient operator overloads.Regards, Daniel--Daniel Freyaixigo AG - financial solutions & technologySchlo?-Rahe-Stra?e 15, 52072 Aachen, Germanyfon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99eMail: da*********@aixigo.de, web: http://www.aixigo.deThe hacks that we write today become the bugs of tomorrow.[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 这篇关于运算符重载和继承:代码批评请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 02:54