shu*_*jan 4 c++ structure class
// By using structure :
struct complex {
float real;
float imag;
};
complex operator+(complex, complex);
main() {
complex t1, t2, t3;
t3 = t1 + t2;
}
complex operator+(complex w, complex z) {
statement 1;
statement 2;
}
// By using class :
class complex {
int real;
int imag;
public:
complex operator+(complex c) {
statement 1;
statement 2;
}
main() {
complex t1, t2, t3;
t3 = t1 + t2;
}
Run Code Online (Sandbox Code Playgroud)
在使用结构时,重载函数可以接受两个参数,而在使用类时,重载函数只接受一个参数,当重载操作符函数在两种情况下都是成员函数时,即在struct和class中.为什么会这样?