运算符重载问题

时间:2010-09-14 00:42:42

标签: c++ operator-overloading

所以我的教授有一个样本.h文件,最后有以下操作符

//ComplexNumber.h

#include <iostream>
using namespace std;

#ifndef MY_COMPLEX_H
#define MY_COMPLEX_H

class complexNumber {
public:
    complexNumber();
    complexNumber(double a, double b);
    void setReal(double a);
    void setImaginary(double b);
    double getReal();
    double getImaginary();
    void printComplex();

private:
    double realPart;
    double imaginaryPart;
};

complexNumber add(complexNumber A, complexNumber B);
complexNumber subtract(complexNumber A, complexNumber B);
complexNumber multiply(complexNumber A, complexNumber B);
complexNumber divide(complexNumber A, complexNumber B);

complexNumber operator +(complexNumber A, complexNumber B);
complexNumber operator -(complexNumber A, complexNumber B);
complexNumber operator *(complexNumber A, complexNumber B);
complexNumber operator /(complexNumber A, complexNumber B);

ostream & operator << (ostream &outs, complexNumber A);

// istream & operator >> (istream &ins, complexNumber &A);

#endif

他获得complexNumber AcomplexNumber B的位置?我不认为这些是变量...

1 个答案:

答案 0 :(得分:4)

ABcomplexNumber类型的函数参数。在不是定义的声明中,名称AB并不意味着什么;以下两个是相同的:

complexNumber add(complexNumber A, complexNumber B);
complexNumber add(complexNumber  , complexNumber  );

最好为参数命名,因为它通常使函数声明更容易理解,并有助于使代码自我记录。

在定义中,如果要使用为给定参数传递的参数,则参数必须具有名称,否则无法识别它。