c++运算符重载调用问题求解答
有如下代码,其中标识1,2,3的三个地方的if语句在此处调用3个不同类型的函数,它在调用对应的函数是怎么区分的,比如按照什么调用类成员运算符函数,类成员函数,和外部普通函...
有如下代码,其中标识1,2,3的三个地方的if语句在此处调用3个不同类型的函数,它在调用对应的函数是怎么区分的,比如按照什么调用类成员运算符函数,类成员函数,和外部普通函数。程式是怎么区别它们进行调用的。请帮忙解答,非常感谢。
#include<iostream>
using std::cout;
using std::endl;
class CBox
{
public:
explicit CBox(double lv=1.0,double wv=1.0,double hv=1.0):
m_Length(lv),m_Width(wv),m_Height(hv)
{
cout<<endl<<"Constructor called.";
}
double Volume()const
{
return m_Length*m_Width*m_Height;
}
bool operator>(const CBox& aBox) const
{
return this->Volume() >aBox.Volume();
}
bool operator>(const double&value)const
{
return this->Volume()>value;
}
~CBox()
{
cout<<"Destructor called."<<endl;
}
private:
double m_Length;
double m_Width;
double m_Height;
};
bool operator>(const double& value,const CBox&aBox);
int main()
{
CBox smallBox(4.0,2.0,1.0);
CBox mediumBox(10.0,4.0,2.0);
if(mediumBox>smallBox)-------------------------------------------------------1.
cout<<endl<<"mediumBox is bigger than smallBox";
if(mediumBox>50.0)------------------------------------------------------------2.
cout<<endl<<"mediumBox capacity is more than 50";
else cout<<endl<<"mediumBox capacity is not more than 50";
if(10.0>smallBox)----------------------------------------------------------------3.
cout<<endl<<"smallBox capacity is less than 10";
else
cout<<endl<<"smallBox capacity is not less than 10";
cout<<endl;
return 0;
}
bool operator>(const double&value, const CBox& aBox)
{
return value>aBox.Volume();
} 展开
#include<iostream>
using std::cout;
using std::endl;
class CBox
{
public:
explicit CBox(double lv=1.0,double wv=1.0,double hv=1.0):
m_Length(lv),m_Width(wv),m_Height(hv)
{
cout<<endl<<"Constructor called.";
}
double Volume()const
{
return m_Length*m_Width*m_Height;
}
bool operator>(const CBox& aBox) const
{
return this->Volume() >aBox.Volume();
}
bool operator>(const double&value)const
{
return this->Volume()>value;
}
~CBox()
{
cout<<"Destructor called."<<endl;
}
private:
double m_Length;
double m_Width;
double m_Height;
};
bool operator>(const double& value,const CBox&aBox);
int main()
{
CBox smallBox(4.0,2.0,1.0);
CBox mediumBox(10.0,4.0,2.0);
if(mediumBox>smallBox)-------------------------------------------------------1.
cout<<endl<<"mediumBox is bigger than smallBox";
if(mediumBox>50.0)------------------------------------------------------------2.
cout<<endl<<"mediumBox capacity is more than 50";
else cout<<endl<<"mediumBox capacity is not more than 50";
if(10.0>smallBox)----------------------------------------------------------------3.
cout<<endl<<"smallBox capacity is less than 10";
else
cout<<endl<<"smallBox capacity is not less than 10";
cout<<endl;
return 0;
}
bool operator>(const double&value, const CBox& aBox)
{
return value>aBox.Volume();
} 展开
1个回答
展开全部
mediumBox>smallBox
比较符两边都是CBox,等同于mediumBox.operator>(smallBox)
因此调用bool operator>(const CBox& aBox) const
mediumBox>50.0
左边是CBox,右边是double,等同于mediumBox.operator>(50.0)因此调用bool operator>(const double&value)const
10.0>smallBox
左边double,右边CBox,但是不存在10.0.operator>(CBox)但是有非成员函数bool operator>(const double&value, const CBox& aBox)
因此调用bool operator>(const double&value, const CBox& aBox)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询