C++运算符重载

操作符核心示例

++与==操作符

class Vector {

private:
    int x;
    int y;
public:
    int getX() {
        return x;
    }
    int getY() {
        return y;
    }

    Vector operator + (Vector& vector) {
        cout << "+运算符重载" << endl;
        this->x = this->x + vector.getX();
        this->y = this->y + vector.getX();
        return Vector(x, y);
    }

    Vector operator ++ (int) {
        cout << "后置++" << endl;
        this->x = this->x + 1;
        this->y = this->y + 1;
        return Vector(x, y);
    }

    Vector operator ++ () {
        cout << "前置++" << endl;
        this->x = this->x + 1;
        this->y = this->y + 1;
        return Vector(x, y);
    }

    bool operator == (const Vector& v) {
        cout << "==运算符重载 "<<this->x << endl;
        cout << "==运算符重载 "<<this->y << endl;
        cout << "==运算符重载 "<<v.x << endl;
        cout << "==运算符重载 "<<v.y << endl;
        bool result = this->x == v.x && this->y == v.y;
        return result;
    }

    Vector(int x, int y) {
        cout << "普通构造函数" << endl;
        this->x = x;
        this->y = y;
    }

    Vector(const Vector& vector) {
        cout << "拷贝构造函数" << endl;
        this->x = vector.x;
        this->y = vector.y;
    }

    ~Vector() {
        cout << "析构函数" << endl;
    }

    friend ostream& operator << (ostream& _Ostr,
        const Vector& v) {
        cout << v.x << "  " << v.y;
        return _Ostr;
    }
};

void main() {
    Vector v1(1, 2);
    Vector v2(1, 2);
    Vector v = v1 + v2;
    v++;
    cout << (v1 == v2) << endl;
    ++v;
    cout << v.getX() << " " << v.getY() << endl;
    cout << v << endl;
    getchar();
}

运行结果

普通构造函数
普通构造函数
+运算符重载
普通构造函数
后置++
普通构造函数
析构函数
==运算符重载 2
==运算符重载 3
==运算符重载 1
==运算符重载 2
0
前置++
普通构造函数
析构函数
4 5
4  5

[]操作符

class Array 
{
public:
    Array(int size) {
        this->size = size;
        this->array = (int *)malloc(sizeof(int)*size);
    }
    ~Array() {
        if (this->array)
        {
            free(this->array);
            this->array = NULL;
        }
    }

    int& operator [](int index) 
    {
        return this->array[index];
        //return *(array + index);
    }

    void set(int index,int value) 
    {
        this->array[index] = value;
        //*(array + index) = value;
    }
    void printArray() 
    {
        for (int i = 0; i < size; i++)
        {
            //cout<<array[i]<<endl;
            cout<<*(array+i)<<endl;
        }
    }
private:
    int size;
    int* array;
};

void main() {
    Array *arr = new Array(6);
    arr->set(0,8);
    arr->set(1,9);
    arr->set(2,10);
    (*arr)[3] = 11;
    (*arr)[4] = 12;
    (*arr)[5] = 13;
    arr->printArray();
    delete(arr);
    getchar();
}

运行结果

8
9
10
11
12
13
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • C++运算符重载-下篇 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符和二元逻辑运算符4...
    Haley_2013阅读 5,330评论 0 49
  • 友元 友元函数 友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成...
    V_梦凡阅读 2,642评论 0 2
  • C++语言的一个很有意思的特性就是除了支持函数重载外还支持运算符重载,原因就是在C++看来运算符也算是一种函数。比...
    欧阳大哥2013阅读 7,625评论 0 8
  • C++运算符重载-上篇 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符和二元逻辑运算符4...
    Haley_2013阅读 6,846评论 0 51
  • 博客示例源代码 重载前++和后++ 一定要注意,i ++和++ i的区别,这里可以试一下i ++++和++++ i...
    StevenHD阅读 1,580评论 0 0

友情链接更多精彩内容