美文网首页
C++ 重载 [] = == !=

C++ 重载 [] = == !=

作者: ag4kd | 来源:发表于2019-10-25 10:12 被阅读0次

    一 重载 [] = == !=

    C++ Primer Plus 第六版-11.2.2章节

    1.1 重载[]

    C++ 规定,下标运算符[ ]必须以成员函数的形式进行重载。该重载函数在类中的声明格式如下:

    读写方式:

    返回值类型 & operator[ ] (参数);
    

    只读模式:

    const 返回值类型 & operator[ ] (参数) const;
    

    返回值类型:为此类类型可以实现链式调用。

    1.2 头文件

    //
    // Created by Biter on 2019/10/24.
    //
    #include <iostream>
    //
    // Created by Biter on 2019/10/24.
    //
    using namespace std;
    
    class Person {
    public:
    
        /**
         * 用来创建 多个 Person对象
         *
         * @param length
         */
        explicit Person(int length = 0);
    
        /**
         * 列表初始化
         * 
         * @param age 
         * @param name 
         */
        Person(int age, string name);
    
        /**
         * 初始化一个对象
         * 
         * @param person 
         */
        Person(Person &person);
    
        ~Person();
    
    public:
        /**
         * 重载 索引运算符
         * @param i 索引
         * @return 此类对象
         */
        Person &operator[](int i);
    
    
        /**
         * 重载赋值运算符
         *
         * @param person
         */
        void operator=(Person &person);
    
        /**
         * 重载赋值运算符
         *
         * @param age 年龄
         */
        void operator=(int age);
    
        /**
         * 重载赋值运算符,设置名字
         *
         * @param name 名字
         */
        void operator=(string name);
    
        /**
         * 重载等于判断运算符,赋值对象
         *
         * @param person
         */
        bool operator==(Person &person);
    
        /**
         * 重载不等于运算符
         * @param person
         */
        bool operator!=(Person &person);
    
        /**
         * 输出对象的属性值
         */
        void show() const;
    
    private:
        int m_age{};
        string m_name{};
        int m_length{0};
        Person *m_p;
    };
    

    1.3 实现文件

    //
    // Created by Biter on 2019/10/24.
    //
    #include "Person.h"
    
    Person::Person(int length) : m_length(length) {
        if (length == 0) {
            m_p = nullptr;
        } else {
            m_p = new Person[length];
        }
    }
    
    Person::Person(Person &person) {
        m_age = person.m_age;
        m_name = person.m_name;
        std::cout << "拷贝构造函数 m_age = " << m_age << "; m_name = " << m_name << std::endl;
    }
    
    Person::~Person() {
     if (m_p != nullptr) {
            std::cout << "析构函数 m_age = " << m_age << "; m_name = " << m_name << "; m_p 地址 = " << m_p << std::endl;
            delete[]m_p;
            m_p = nullptr;
        } else{
            std::cout << "析构函数 m_age = " << m_age << "; m_name = " << m_name << "; 地址 = " << this << std::endl;
        }
    }
    
    void Person::show() const {
        for (int i = 0; i < m_length; i++) {
            std::cout << "Person[" << i << "].m_age = " << m_p[i].m_age << std::endl;
            std::cout << "Person[" << i << "].m_name = " << m_p[i].m_name << std::endl;
        }
    }
    
    void Person::operator=(Person &person) {
        m_age = person.m_age;
        m_name = person.m_name;
    }
    
    bool Person::operator==(Person &person) {
        return m_age == person.m_age;
    }
    
    bool Person::operator!=(Person &person) {
        return m_age != person.m_age;
    }
    
    Person &Person::operator[](int i) {
        return m_p[i];
    }
    
    void Person::operator=(int age) {
        m_age = age;
    }
    
    Person::Person(int age, const string name) {
        m_age = age;
        m_name = name;
    }
    
    void Person::operator=(string name) {
        m_name = name;
    }
    

    1.4 程序代码

    #include "Person.h"
    
    int main() {
        int mArraySize = 2;
    
        Person myOperator(mArraySize);
        Person person{23, "biter"};
    
        Person person1(person);
        myOperator[0] = person;
    
        myOperator[1] = 1;
        myOperator[1] = "binny";
    
    
        myOperator.show();
    
        std::cout << "是否相等?" << (myOperator[0] == myOperator[1]) << std::endl;
        std::cout << "是否不相等?" << (myOperator[0] != myOperator[1]) << std::endl;
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++ 重载 [] = == !=

          本文链接:https://www.haomeiwen.com/subject/ysxsvctx.html