本文介绍了为重载运算符派生类的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图派生一个新类,它将添加新函数但没有新的数据成员和基类重载运算符 (+, - ,+ =, - =等......)返回(Base&)或(const Base),取决于运营商的: 类派生:公共基地 { }; 出现问题如果我做以下 派生d1,d2; 派生d3 = d1 + d2; 什么应该是更好的方法吗? 只需添加 派生(const Base&)和 派生& operator =(const基础&) 或者应该更好地重新声明派生类中的所有运算符(它们有很多 )并且除了调用之外什么都不执行 基类版本并返回派生参考或对象。 问候, Olivier Langlois http://www3.sympatico.ca/olanglois [见 http://www.gotw.ca/resources/clcm.htm 有关的信息] [comp.lang .C ++。主持。第一次海报:做到这一点! ]Hi,I am trying to derive a new class that will add new functions but nonew data members and the base class has overloaded operators(+,-,+=,-=,etc...) returning either (Base &) or (const Base) dependingon the operator:class Derived : public Base{};The problem occurs if I do the followingDerived d1,d2;Derived d3 = d1+d2;what should be the better approach?Just addDerived(const Base&) andDerived &operator=(const Base&)or should it be better to redeclare all the operators (there is a lotof them) in the derived class and perform nothing except calling thebase class version and return Derived reference or object.Greetings,Olivier Langlois http://www3.sympatico.ca/olanglois[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ]推荐答案 我不觉得它怎么能帮助_unless_你正在预见需要 ''派生''以某种方式在未来修改操作(即使它确实需要现在不需要它)。界面介绍应该尽早完成 而不是以后。 V - 请在通过电子邮件回复时删除资金''A' 我没有回复最热门的回复,请不要问I don''t think how it could help _unless_ you''re foreseeing the need for''Derived'' to amend the operation somehow in the future (even if it doesnot need it now). Introduction of an interface should be done soonerrather than later.V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask IMO如果可以的话,总是重载派生版本的运算符, 否则每个操作实际上都会丢失类型信息和 可能是派生出额外的数据。事实上,不要为基类重载它们 ,所以如果你没有,你会得到一个错误。当然那就是 更多工作......所以你可以尝试一下; http://www.boost.org/libs/utility/operators.htm 这是意味着减少实施普通 运营商所需的工作。 (不,我自己没有用过) 问候 Andy Little [见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息] [comp.lang.c ++。moderated。第一次海报:做到这一点! ]IMO Always overload the operators for the derived versions if you can,otherwise each operation is effectively losing type information andpossibly the deriveds extra data. In fact dont bother overloading themfor the base class so you get an error if you havent. Of course thatsmore work... so you could try looking at ; http://www.boost.org/libs/utility/operators.htmwhich is meant to reduce the work required to implement commonoperators.(No I havent used it myself)regardsAndy Little[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 你几乎肯定不希望通过公开推导来做这件事,因为我 非常怀疑Base被设计为一个基类(即我希望它缺少一个虚拟dtor的b $ b) 为什么你想要一个类是除了一些 添加的功能之外,与Base相同?为什么不添加一些免费功能?你派生的 类没有对基类的特殊访问权限,所以它的成员 函数不能做任何免费函数无法做的事情。 在我给你任何进一步指导之前,我需要知道你为什么要这样做。 你在做什么。 - Francis Glassborow ACCU ''你可以做到!'的作者''看 http://www.spellen.org/youcandoit 对于项目构想和贡献: http://www.spellen.org/youcandoit/projects [见 http://www.gotw.ca/resources/clcm.htm 有关的信息] [comp.lang.c ++。moderated。第一次海报:做到这一点! ]You almost certainly do not want to do this by public derivation as Ivery much doubt that Base was designed as a base class (i.e. I expect itto lack a virtual dtor)Why do you want a class that is identical to Base other than for someadded functionality? Why not just add some free functions? Your derivedclass will have no special access to the base class so its memberfunctions cannot do anything that free functions could not do.Before I give you any further guidance I need to know why you want to dowhat you are doing.--Francis Glassborow ACCUAuthor of ''You Can Do It!'' see http://www.spellen.org/youcandoitFor project ideas and contributions: http://www.spellen.org/youcandoit/projects[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 这篇关于为重载运算符派生类的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 09:16