重载除法运算哪种效率高一些?望高手指教
class OB
{
public:
OB(int i):x(i){;}
const int & operator/(const OB & t);
private:
int x;
};
void main()
{
OB a(443);
OB b(16);
int num = a/b;
}
const int & OB::operator/(const OB & t)//函数定义1
{
return x/t.x+1;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const int & OB::operator/(const OB & t)//函数定义2
{
int temp = this->x;
for (int i = 0;temp>=t.x;i++)
{
temp=temp-t.x;
}
return ++i;
}
[解决办法]
int的应该影响不大,如果是类的对象,就是第一种,第二种还要声明一个变量,主要的是如果是类返回的是一个对象,这样就会多调用构造和析构函数