类定义(class definition)
class以外的函数
-
存在的一定是函数
-
如果是
class
的函数,那么一定带有类名::function
-
要么就是全局函数,函数名称不会带有
class_name_function_name
非成员函数(无this)
inline complex
operator + (const complex& x, const complex& y)
{
return complex(real(x) + real(y), imag(x) + imag(y));
}
inline complex
operator + (const complex& x, double y)
{
return complex(real(x) + y, imag(y));
}
inline complex
operator + (double x, const complex& y)
{
return complex(x + real(y), imag(y));
}
上述的例子当中进行相加完成以后并没有一个object
-> 所以返回的是一个value
-> 所以在return
的时候不能return by reference
临时对象 -> typename ()
-> 声明周期到下一行就结束了.使用这个声明方法可以声明temp object
示例代码: