为重载运算符派生类的正确方法是什么? [英] What is the correct way to derive a class in regard to overloaded operators

查看:79
本文介绍了为重载运算符派生类的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图派生一个新类,它将添加新函数但没有新的数据成员和基类重载运算符

(+, - ,+ =, - =等......)返回(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 no
new data members and the base class has overloaded operators
(+,-,+=,-=,etc...) returning either (Base &) or (const Base) depending
on the operator:

class Derived : public Base
{
};

The problem occurs if I do the following

Derived d1,d2;
Derived d3 = d1+d2;

what should be the better approach?

Just add
Derived(const Base&) and
Derived &operator=(const Base&)

or should it be better to redeclare all the operators (there is a lot
of them) in the derived class and perform nothing except calling the
base 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! ]

推荐答案

ol ** *****@sympatico.ca 写道:
我试图派生一个新的类,它将添加新的函数但没有新的数据成员和基类有重载操作符
(+, - ,+ =, - =等等)返回(Base&)或(const Base)依赖于操作符:
类派生:公共基地
{
};

如果我做以下事情就会出现问题

派生的d1,d2;
派生d3 = d1 + d2;

什么应该是更好的方法?

只需添加
Derived(const Base&)和
Derived& operator =(const Base&)


如果''Derived''从来不知道正在执行添加等,

则是,它应该足够了。

或者在派生类中重新声明所有运算符(它们中有很多运算符)并且应该更好什么都没有,除了调用
基类版本并返回Derived引用或对象。
I am trying to derive a new class that will add new functions but no
new data members and the base class has overloaded operators
(+,-,+=,-=,etc...) returning either (Base &) or (const Base) depending
on the operator:

class Derived : public Base
{
};

The problem occurs if I do the following

Derived d1,d2;
Derived d3 = d1+d2;

what should be the better approach?

Just add
Derived(const Base&) and
Derived &operator=(const Base&)
If ''Derived'' never has to know that addition, etc., is being performed,
then yes, it should be sufficient.
or should it be better to redeclare all the operators (there is a lot
of them) in the derived class and perform nothing except calling the
base class version and return Derived reference or object.




我不觉得它怎么能帮助_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 does
not need it now). Introduction of an interface should be done sooner
rather than later.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



ol*******@sympatico.ca 写道:


我正在尝试派生一个新类,它将添加新的函数,但没有新的数据成员,基类有重载的运算符
( +, - ,+ =, - =等......)返回(Base&)或(const Base)取决于运算符:

类派生:public Base
{
};

如果我执行以下操作就会出现问题

派生的d1,d2;
派生的d3 = d1 + d2;

什么应该是更好的方法?

只需添加
Derived(const Base&)和
Derived& operator =(const Base&)

或者我应该最好在派生类中重新声明所有运算符(它们中有很多运算符),除了调用
基类版本并返回Derived引用或对象之外什么都不执行。
Hi,

I am trying to derive a new class that will add new functions but no
new data members and the base class has overloaded operators
(+,-,+=,-=,etc...) returning either (Base &) or (const Base) depending
on the operator:

class Derived : public Base
{
};

The problem occurs if I do the following

Derived d1,d2;
Derived d3 = d1+d2;

what should be the better approach?

Just add
Derived(const Base&) and
Derived &operator=(const Base&)

or should it be better to redeclare all the operators (there is a lot
of them) in the derived class and perform nothing except calling the
base class version and return Derived reference or object.




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 and
possibly the deriveds extra data. In fact dont bother overloading them
for the base class so you get an error if you havent. Of course thats
more work... so you could try looking at ;

http://www.boost.org/libs/utility/operators.htm

which is meant to reduce the work required to implement common
operators.
(No I havent used it myself)

regards
Andy Little
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


文章< 11 ********************* @ j55g2000cwa.googlegroups 。 com>,
ol*******@sympatico.ca 写道
In article <11*********************@j55g2000cwa.googlegroups. com>,
ol*******@sympatico.ca writes


我正在尝试派生一个新的类,它将添加新的函数,但没有新的数据成员,基类有重载的运算符
(+, - ,+ =, - =等...)返回(Base&)或(const Base)取决于运营商:

类派生:公共基地
{
};

如果我执行以下操作就会出现问题

派生的d1,d2;
派生的d3 = d1 + d2;

什么应该是更好的方法?
Hi,

I am trying to derive a new class that will add new functions but no
new data members and the base class has overloaded operators
(+,-,+=,-=,etc...) returning either (Base &) or (const Base) depending
on the operator:

class Derived : public Base
{
};

The problem occurs if I do the following

Derived d1,d2;
Derived d3 = d1+d2;

what should be the better approach?




你几乎肯定不希望通过公开推导来做这件事,因为我

非常怀疑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 I
very much doubt that Base was designed as a base class (i.e. I expect it
to lack a virtual dtor)

Why do you want a class that is identical to Base other than for some
added functionality? Why not just add some free functions? Your derived
class will have no special access to the base class so its member
functions cannot do anything that free functions could not do.

Before I give you any further guidance I need to know why you want to do
what you are doing.
--
Francis Glassborow ACCU
Author of ''You Can Do It!'' see http://www.spellen.org/youcandoit
For 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! ]


这篇关于为重载运算符派生类的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆