运算符重载和继承:代码批评请 [英] Operator overloading and inheritence: Code critique please

查看:88
本文介绍了运算符重载和继承:代码批评请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在玩操作符重载和继承,

专门重载基类中的+运算符及其

派生类。


结构很简单:基类有两个int memebersdataA,

" dataB"。派生类有一个额外的int成员dataC。我只是试图重载+运算符以便''添加''两个对象

将总结相应的int成员。


有人能告诉我,如果我在

派生类中正确地重载了​​+运算符,或者有更好的方法吗?


#include< iostream>

using namespace std;


class base

{

public:

// base int members

int dataA,dataB;


//默认构造函数

base():dataA(0),dataB(0)

{}


//重载构造函数

base(int x,int y):dataA(x),dataB(y)

{}


//基类中的operator +

base operator +(const base& rhs)const

{

base tmp;

tmp.dataA = this - > dataA + rhs.dataA;

tmp.dataB = this-> dataB + rhs.dataB;


返回tmp;

}


} ;


等级派生:公共基础

{

public:

// additonal派生类int成员

int dataC;


//默认构造函数

derived():base()

{

dataC = 0;

}


//重载构造函数

派生( int x,int y,int z):base(x,y)

{

dataC = z;

}


//复制构造函数

派生(const derived& t):base(t)

{

this-> dataC = t.dataC;

}

//派生类中的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写道:
< blockquote class =post_quotes>我正在玩操作符重载和继承,
专门重载基类中的+运算符及其派生类。

结构是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 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.

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;

//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;
}

};

class derived: public base
{
public:
//additonal derived class int member
int dataC;

//default constructor
derived():base()
{
dataC=0;
}

//overloaded constructor
derived(int x, int y, int z):base(x,y)
{
dataC=z;
}

//copy constructor
derived(const derived& t): base(t)
{
this->dataC = t.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;
}

//print values
void 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 33

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.

-Thanks
Gorda 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 quick
response for speed:

- No public members please :)

- I recommend that you implement operator+ as a free
function 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 chances
of 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;
}

Ali


On 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 this
case. At least for the example given below, it seems to be better if
''derived'' contains ''base'' as a member. You need to ask yourself
whether ''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 free
function 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 chances
of 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 you
have finished this, have a look at Boost.org''s operators library
<http://www.boost.org/libs/utility/operators.htm> to see how some parts
can be even more generalised and for further advice on how to implement
correct and efficient operator overloads.

Regards, Daniel

--
Daniel Frey

aixigo AG - financial solutions & technology
Schlo?-Rahe-Stra?e 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: da*********@aixigo.de, web: http://www.aixigo.de

The 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! ]


这篇关于运算符重载和继承:代码批评请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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