Gibt es irgendwelche "Regeln" für welche Art von Rückgabetyp Member vs Non-Member-Funktionen haben sollte?Member vs Nichtmitgliedsfunktionen, Rückkopie oder Referenz?
Ich baue eine komplexe Klasse und ich dachte, Elementfunktionen würden Rückgabetyp Referenzen sein und Nichtmitglied würde Kopien sein. Ist das richtig?
complex.h Referenz:
#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
Verwandt, Sie könnten wahrscheinlich einige ziemlich gute Richtlinien aus der Klasse ['std :: complex'] (http://en.cppreference.com/w/cpp/numeric/complex) bekommen, die Sie scheinbar neu erfinden. Der Zufall ist die Standard-Bibliothek Architekten haben einige durchdachte Entscheidungen getroffen. – WhozCraig