二进制+运算符重载c ++

时间:2014-11-27 16:15:45

标签: c++ operator-overloading

我刚学习C ++,所以提供的任何帮助都会很棒。我试图在我的代码中重载二元运算符+:这是我的实现。

我使用的编译器是Mac OS X上的Xcode 6.1。

我收到一个编译错误,隐式删除了复制赋值运算符,我在main函数中使用了+运算符。

#include <iostream>
#include <string>

using namespace std;

class Person{

private:
string name;
int age;
double height;

public:
Person(string n="Haroon", int a=23, double h=72.1){
    this->name = n;
    this->age = a;
    this->height = h;
};

int getAge() const { return this->age; }
double getHeight() const { return this->height; }
string getName() const { return this->name; }

void setHeight(double x){
    this->height = x;
}

Person operator+ (const Person &rhs){

    string a = this->getName();
    string b = rhs.getName();

    string name = a+b;

    int c = max(this->getAge(),rhs.getAge());
    int d = max(this->getHeight(),rhs.getHeight());

    Person p(name,c,d);
    return p;
}

~Person(){};
};

int main(int argc, const char * argv[]) {

Person a("Abc",1,2);
Person b("Def",5,65);

Person n;
n = a+b;

return 0;
}

1 个答案:

答案 0 :(得分:-1)

  1. 原因在于:

    人n; //您正在调用默认构造函数。你会得到一个人(“Haroon”,23,72.1)

    n = a + b; //您正在调用一个没有重载的赋值运算符

  2. C ++有默认构造函数,转换构造函数和复制构造函数。

    [~/testing/constructor]$ cat main.cpp
    
    #include <iostream>
    
    class A
    {
    public:
    
    A()      : value(0) { std::cout << "Default " << value << std::endl; }
    A(int a) : value(a) { std::cout << "Convert " << value << std::endl; }
    A(const A& other) : value(other.value) { std::cout << "Copy    " << value << std::endl; }
    
    private:
        int value;
    };
    
    int main()
    {
        A x;
        A y(10);
        A z = y;
        A w(x);
    }
    
    
    [~/testing/constructor]$ ./run
    Default 0
    Convert 10
    Copy    10
    Copy    0
    

    这里x默认构造,y是转换。请注意,A z = y;A w(x);都是复制构造函数。它们完全一样!!但是您的n = a + b;是分配运算符。

  3. 使用初始化而不是构造函数中的赋值。 : value(0)是初始化。这是Scott Meyers“Effective C ++”一书中的成语。

  4. 如果您正在编写类的成员函数,则可以在C ++中省略this->。您使用的变量是类或函数局部变量或全局变量的成员变量。 (如果名称有冲突,使用优先级本地&gt;成员&gt;全局变量)例如。

    [~/testing/var]$ cat main.cpp
    #include <iostream>
    
    int a;
    
    class A
    {
    public:
        A()      : a(0) {}
        A(int x) : a(x) {}
        ~A() {}
    
        void f() { int a = 9; std::cout << a << std::endl; }
    private:
        int a;
    };
    
    int main()
    {
        A x(5);
        x.f();
    }
    [~/testing/var]$ ./run
    9
    
  5. 对于你的班级,我会写这样的正式C ++格式:

    class Person{
    
    public: // constructor & destructor
        Person(string n="Haroon", int a=23, double h=72.1)
            : name(n)
            , age(a)
            , height(h)
        {
        }
        Person(const Person& other)
            : name(other.name)
            , age(other.age)
            , height(other.height)
        {
        }
    
    public: // operators
    
        Person& operator=(const Person& other)
        {
            if (&other == this) return *this;  // Such as using as "a = a"
    
            name   = other.name;
            age    = other.age;
            height = other.height;
    
            return *this;
        }
    
        Person operator+(const Person& other)
        {
            string n = name + other.name;  // You don't have to use other.getName() because C++ private variable is accessible by self class and friend class
            age    a = max(age, other.age);
            height h = max(height, other.height);
    
            return Person(n, a, h);
        }
    public: // setter & getter
    
        int    getAge()    const { return age;    }
        double getHeight() const { return height; }
        string getName()   const { return name;   }
    
        void setHeight(double x) { height = x; }
    
    private:
        string name;
        int    age;
        double height;
    
    };