建设社团网站的可行性分析,青岛快速排名,999导航,怎样进入wordpress//单独形式
x x y; x x - y;
//也可以写为复合形式
x y; x - y;效率问题
一般而言#xff0c;复合操作符比其对应的单独形式效率高#xff1a;因为单独形式需要返回一个新的对象#xff0c;就会产生一个临时对象的构造和析构成本#xff0c;复合版本则是直接写入左…//单独形式
x x y; x x - y;
//也可以写为复合形式
x y; x - y;效率问题
一般而言复合操作符比其对应的单独形式效率高因为单独形式需要返回一个新的对象就会产生一个临时对象的构造和析构成本复合版本则是直接写入左端自变量不需要产生一个临时对象来放置返回值。同时提供复合和单独形式允许客户端在便利和效率之间抉择单独形式调用T的复制构造函数它建立了临时对象与rhs一起调用运算结果从operator返回。这样会比使用命名对象效率更高因为使用了返回值最优化的方法RVO。
//operator根据operator来实现
const Rational operator(const Rational lhs,const Rational rhs)
{return Rational(lhs) rhs;
}
//operator-根据operator-来实现
const Rational operator-(const Rational lhs,const Rational rhs)
{return Rational(lhs) - rhs;
}templatetypename T
const T operator (const T lhs,const T rhs)
{return T(lhs) rhs;
}templatetypename T
const T operator (const T lhs,const T rhs)
{return T(lhs) rhs;//相比于//T result(lhs);//return T(lhs) rhs;
}综上
operator的复合形式operator比单独形式operator效率更加高开发时优先考虑使用复合形式。