對於什麼樣的返回類型成員與非成員函數應該有什麼「規則」?會員與非會員功能,退貨複印或參考?
我正在構建一個複雜類,我認爲成員函數是返回類型引用,非成員將是副本。它是否正確?
Complex.h供參考:
#include <iostream>
#include <string>
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex {
public:
double real;
double imaginary;
Complex();
Complex(const double real);
Complex(const double real, const double imaginary);
Complex(const Complex &rhs);
Complex& operator *= (const Complex&);
Complex& operator += (const Complex&);
Complex& operator /= (const Complex&);
Complex& operator -= (const Complex&);
Complex& operator = (const Complex&);
};
class Complex;
double abs(const Complex& c);
Complex operator * (const Complex& c1, const Complex& c2);
Complex operator/(const Complex& c1, const Complex& c2);
Complex operator + (const Complex& c1, const Complex& c2);
Complex operator - (const Complex& c1, const Complex& c2);
bool operator == (const Complex& c1, const Complex& c2);
bool operator != (const Complex& c1, const Complex& c2);
std::istream& operator >> (std::istream& in, Complex& c);
std::ostream& operator << (std::ostream& out, const Complex& c);
Complex operator ""_i(long double arg);
Complex operator ""_i(unsigned long long arg);
#endif
相關的,你可能會從你似乎正在重塑的['std :: complex'](http://en.cppreference.com/w/cpp/numeric/complex)類中獲得一些相當不錯的指導。標準圖書館建築師有機會做出一些經過深思熟慮的選擇。 – WhozCraig