开发者

C++类中的运算符重载过程

目录
  • 为什么要实现运算符重载?
  • 1.加法运算符重载
    • 在类内实现加法运算符重载
    • 在类外实现加法运算符重载
  • 2.左移运算符重载
    • 3.递增运算符重载
      • 4.+=运算符重载
        • 5.关系运算符和赋值运算符重载
          • 总结

            为什么要实现运算符重载?

            在 C++ 中,运算符最初是为内置类型(如intdouble等)定义操作方式。当定义一个新的类(自定义类型)时,编译器并不清楚如何对这个新类型的成员变量应用运算符。

            通过运算符重载,程序员可以明确地告诉编译器对于该类的对象,运算符(如+-*等)应该如何操作其成员变量,从而使自定义类型能够像内置类型一样自然地使用这些运算符。

            1.加法运算符重载

            作用:实现两个自定义类型数据的加法运算

            在类内实现加法运算符重载

            #include <IOStream>
            using namespace std;
            class Person {
                int age;
                int money;
            
            public:
                Person() : money(0), age(0) {}
                Person(int val, int val2) : age(val), money(val2) {}
                Person(const Person& other) {
                    this->age = other.age;
                    this->money = other.money;
                }
                // 类内实现+重载 本质:Person p3=p1.operator+(p2)
                Person operator+(const Person& other) {
                    Person p;
                    p.age = this->age + other.age;
                    p.money = this->money + other.money;
                    return p; // 不以引用返回是因为,在执行完这个函数后,p会被销毁,以值传递会调用拷贝构造
                }
                void print() { cout << age << " " << money << endl; }
            };
            
            int main() {
                Person a(18, 50);    // 括号法
                Person b = {12, 50}; // 隐式转换法
                Person c = a + b;
                c.print();
                return 0;
            }

            实现加法运算符重载的operator+函数来完成的,传入的参数为const Person& other的原因有以下的两点:

            • 1.以引用的方式传递是为了防止调用拷贝构造
            • 2.加const修饰是为了防止修改实参

            在函数中声明了一个Person的对象p,返回值是以值的形式返回,因为值返回会调用拷贝构造,如果是以引用的方式传递,当这个函数结束时,对象p就会被销毁掉。

            在类外实现加法运算符重载

            #include <iostream>
            using namespace std;
            class Person {
                int age;
                int money;
                friend Person operator+(const Person& other1, const Person& other2);
            
            public:
                Person() : money(0), age(0) {}
                Person(int val, int val2) : age(val), money(val2) {}
                Person(const Person& other) {
                    this->age = other.age;
                    this->money = other.money;
                }
                void print() { cout << age << " " << money << endl; }
            };
            // 类外实现+重载 本质:Person p3=operator+(p1,p2)
            Person operator+(const Person& other1, const Person& other2) {
                Person p;
                p.age = other1.age + other2.age;
                p.money = other1.money + other2.money;
                return p;
            }
            int main() {
                Person a(18, 50);    // 括号法
                Person b = {12, 50}; // 隐式转换法
                Person c = a + b;
                c.print();
                return 0;
            }

            在类外实现加法运算符重载,相当于类外函数访问类内的成员变量,所以要将这个函数在类中声明为友元函数(friend)。其他的跟在类内实现加法运算符重载是一样的。

            本质:在类内实现加法运算符重载的本质是:Person p3=p1.operator(p2)

            在类外实现加法运算符重载的本质是:Person p3=operator+(p1,p2)

            因为一个是通过对象去调用这个函数,它本身也算是一个参数,所以只需要传人一个参数。而在类外,则是相当于直接调用这个函数,所以传入的参数是两个。

            无论是再类内还是在类外实现加法运算符重载,最后调用的方式都是+。

            2.左移运算符重载

            作用:可以输出自定义数据类型

            要在类外实现,因为在类内的话,调用格式为:对象.operator();无法达到cout<<

            #include <iostream>
            using namespace std;
            class Person {
                int age;
                int money;
            
            public:
                friend ostream& operator<<(ostream& o, const Person& p);
                Person() : age(0), money(0) {}
                Person(int val, int val2) : agejavascript(val), money(val2) {}
                Person(const Person& other) {
                    cout << "调用拷贝构造";
                    this->age = other.age;python
                    this->money = other.money;
                }
                // 一般不在函数内进行左移重载
                /*void operator<<(ostream& cout,const Person p) {
            
                }*/
            };
            // 类外实现左移运算符重载
            ostream& operator<<(ostream& o, const Person& p) {
                o << p.age << " " << p.money << endl;
                return o;
            }
            int main() {
                Person a(5, 12), b;
                cout << a << b << endl;
                return 0;
            }

            因为是在类外实现的,所以在类内要将其设置为友元函数。参数为两个,一个为ostream类型的o,另外一个为引用类型的对象。

            返回值为引用类型的ostream类型的ostream,因为如果想要进行连续的输出,就必须让前一个的结果作为第二个左移运算符的第一个参数,如下图:

            C++类中的运算符重载过程

            在o<<p.age的返回值为o,然后它与后面就变成了o<<" ",接着又调用这个运算符,这样才能完成连续的输出。

            3.递增运算符重载

            递增分为两种一种是前++,一种是后++。前++是在使用之前就让这个数加1,返回的是加1之后的结果。而后++是先返回这个数再进行加一操作。

            前++返回的是引用,后++返回的是值。

            #include <iostream>
            using namespace std;
            class Person {
                int age;
                int money;
            
            public:
               编程 Person() : age(0), money(0) {}
                Person(int val, int val2) : age(val), money(val2) {}
                Person(const Person& other) {
                    this->age = other.age;
                    this->money = other.money;
                }
                // 前置++的重载  返回引用
                Person& operator++() { // 保证是对一个数进行++
                    this->age++;
                    this->money++;
                    return *this;
                }
                // 后置++的重载 加一个int参数占位符区分 返回值
                Person operator++(int) {
                    Person temp = *this; // 用一个局部变量返回++之前的结果
                    this->age++;
                    this->money++;
                    return temp;
                }
                void print() { cout << age << " " << money; }
            };
            int main() {
                Person p(5, 12);
                ++p;
                p.print();
                return 0;
            }

            对前++和后++都是operator++,所以为了区分,对于后++来说,需要加一个参数占位符来进行区分。

            在返回值这块,前++返回的是引用,因为要的是它进行加一之后的结果,所以可以直接返回这个对象。

            而后++则返回的是一个值,因为要返回的是它加一之前的结果,所以需要一个局部变量返回++之前的结果。

            4.+=运算符重载

            #include <iostream>
            using namespace std;
            class Person {
                int age;
                int money;
            
            public:
                friend ostream& operator<<(ostream& o, const Person& p);
                Person() : age(0), money(0) {}
                Person(int val, int val2) : age(val), money(val2) {}
                Person(const Person& other) {
                    this->age = other.age;
                    this->money = other.money;
                }
                Person& operator+=(const Person& other) {
                    this->age += other.age;
                    this->money += other.money;
                    return *this;
                }
                void print() { cout << age << " " << money; }
            };
            ostream& operator<<(ostream& o, const Person& p) {
                o << p.age << " " << p.money << endl;
                return o;
            }
            int main() {
                Person a(1, 1), b(2, 2), c(3, 3);
                a += b += c;
                cout << a << b << c;
                return 0;
            }

            +=运算符的返回值也是对象本身,因为会涉及到连续+=的情况,和左移运算符的重载类似。

            这样才能实现连续+=的实现。如上面代码运行的android结果为:

            C++类中的运算符重载过程

            要注意运算的顺序是从右往左进行计算的。

            5.关系运算符和赋值运算符重载

            #include <iostream>
            using namespace std;
            class A {
                int num;
                int* p;
            
            public:
                A() : num(0), p(nullptr) {}
                A(int x) : num(x), p(new int(num)) {}
                A(const A& other) { this->num = other.num; }
                bool operator>(const A& other) {
                    if (this->num > other.num)
                        return 1;
                    else
                        return 0;
                }
                bool operator==(const A& other) {
                    if (this->num == other.num)
                        return 1;
                    else
                        return 0;
                }
                A& operator=(const A& other) {
                    if (p)
                        delete p; // 释放原来的堆区内存,拷贝构造没有这一步
                    num = other.num;
                    p = new int(num); // 指向新的堆区内存
                    return *this;
                }
            };
            int& fun() {
                int a = 2;
                return a;
            }
            int main() {
                int x = fun();  // 行
                int& y = fun(); // 不行
                A a(2), b(3), c;
                cout << (a == b);
                a = b =c; // 如果类内没有实现赋值运算符,编译器则会提供默认的赋值运算符,每个成员变量都会被赋值,此时要注意浅拷贝问题
                return 0;
            }

            关系运算符主要包括==、>、<等,他们的返回值都是bool类型。

            赋值运算符,就是把other的值给调用它的对象,如果不对赋值运算符进行重载的话,那么它就会是简单的赋值操作,这里就会导致浅拷贝的问题(可以看作者前面提到的浅拷贝和深拷贝问题)。所以我们就需要对它进行重载。

            在上述代码中,因为p是一块指向堆区的指针变量,所以在进行赋值运算符重载时,需要开辟一块新的堆区,然后保证两个堆区的内容一样。同时,还有保证之前这个指针变量p有没有指向别的堆区内容,如果有,就将它释放,避免内存泄漏。返回值为引用类型,返回对象本身。

            注意:赋值运算符也是编译器给一个类至少添加的函数。

            总结

            以上为个人经验,希望能给大家一个参考,也希望大家python多多支持编程客栈(www.devze.com)。

            0

            上一篇:

            下一篇:

            精彩评论

            暂无评论...
            验证码 换一张
            取 消

            最新开发

            开发排行榜