C++运算符重载

简介: C++运算符重载

C++运算符重载


对已经有的运算符重新进行定义,赋予其另一个种功能,以适应不同的数据类型

加号运算符重载

class Person{
public:
int m_A;
int m_B;
}

可以自己写成员函数来实现两个对象相加

Person PersonAddPerson(Person &p){
Person temp;
temp.m_A=this->m_A+p.m_A;
return temp;
}
Person operator+(Person &p){
Person temp;
temp.m_A=this->m_A+p.m_A;
return temp;
}

通过全局函数实现

Person operator+(Person &p1,Person &p2){
Person temp;
temp.m_A=p1.m_A+p2.m_A;
return temp;
}

左移运算符重载

只能用全局运算符重载<<运算符

ostream & operator<<(ostream &cout,Person &p){
return cout;
}

递增运算符重载

1.

分为重载前置和后置

2.

#include<iostream> 
using namespace std;
class MyInteger{
friend ostream& operator<<(ostream &cout,MyInteger &myint);
public: 
    MyInteger()
    {
        m_Num=0;
    }
    MyInteger& operator++(){      //Ç°ÖÃ++ÔËËã·ûÖØÔØ 
        m_Num++;
        return *this;
    }
    MyInteger& operator++(int){      //ºóÖÃ++ÔËËã·ûÖØÔØ 
        m_Num++;
        return *this;
    }
private:
    int m_Num; 
};
ostream& operator<<(ostream& cout,MyInteger &myint){
    cout <<myint.m_Num;
    return cout;    
} 
int main(){
    MyInteger myint;
    cout <<myint<<endl;  //ûÓÐÖØÔØʱ£º[Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'MyInteger')
    cout <<++myint<<endl;
    cout <<myint++<<endl;
    return 0;
} 

赋值运算符重载

#include<iostream>
using namespace std;
class Person{
    public:
        Person(int age){
            m_Age=new int(age);
        }
        ~Person(){
            if(m_Age!=NULL){
                delete m_Age;
                m_Age=NULL;
            }
        }
        int *m_Age;
    void operator=(Person &p){
        if(m_Age!=NULL){
            delete m_Age;
            m_Age=NULL;
        }
        m_Age=new int(*m_Age);
    }
};
void test01(){
    Person p1(19);
    Person p2=p1;
    cout <<"p1的年龄是:"<<*p1.m_Age<<endl;
    cout <<"p2的年龄是:"<<*p2.m_Age<<endl; 
} 
int main(){
    test01();
    system("pause");
    return 0;
}

关系运算符重载

#include<iostream>
using namespace std;
class Person{
public:
    string m_Name;
    int m_Age;
    Person(string name,int age){
        m_Name=name;
        m_Age=age;
    }   
    bool operator==(Person &p){
        if(this->m_Name==p.m_Name&&this->m_Age==m_Age)
        return true;
        return false;
    }
};
int main(){
    Person p1("Peter",19);
    Person p2("Ben",20);
    if(p1==p2)
    cout <<"p1等于p2"<<endl;
    else
    cout << "p1不等于p2"<<endl;
    return 0; 
}

函数调用运算符重载

函数调用运算符()也可以重载

由于重载后使用方式非常像函数的调用,因此成为仿函数

仿函数没有固定写法,非常灵活

#include<iostream>
using namespace std;
#include<string> 
class Myprint{
    public:
        void operator()(string test){
            cout <<test<<endl;
        }
};
int main(){
    Myprint p;
    p("liwanzhou");
    return 0;
}
相关文章
|
4月前
|
编译器 C++
C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
42 1
|
5月前
|
程序员 编译器 C++
C++中的运算符重载(Operator Overloading)
C++中的运算符重载(Operator Overloading)
52 1
|
1月前
|
C++
C++(十五) 运算符重载
C++中的运算符重载允许对已有运算符的功能进行重新定义,从而扩展语言功能、简化代码并提升效率。重载遵循特定语法,如 `friend 类名 operator 运算符(参数)`。重载时需注意不可新增或改变运算符数量、语义、优先级、结合性和返回类型。常见示例包括双目运算符 `+=` 和单目运算符 `-` 及 `++`。输入输出流运算符 `&lt;&lt;` 和 `&gt;&gt;` 也可重载。部分运算符只能作为成员函数重载。
|
4月前
|
存储 编译器 C++
【C++】:拷贝构造函数和赋值运算符重载
【C++】:拷贝构造函数和赋值运算符重载
26 1
|
4月前
|
C++ 索引
C++核心技术要点《运算符重载》
C++核心技术要点《运算符重载》
52 2
|
3月前
|
自然语言处理 程序员 C++
C++基础知识(五:运算符重载)
运算符重载是C++中的一项强大特性,它允许程序员为自定义类型(如类或结构体)重新定义标准运算符的行为,使得这些运算符能够适用于自定义类型的操作。这样做可以增强代码的可读性和表达力,使得代码更接近自然语言,同时保持了面向对象编程的封装性。
|
3月前
|
Java 程序员 C++
|
3月前
|
编译器 C++
【C++】详解运算符重载,赋值运算符重载,++运算符重载
【C++】详解运算符重载,赋值运算符重载,++运算符重载
|
4月前
|
编译器 C++
【C++】类和对象③(类的默认成员函数:赋值运算符重载)
在C++中,运算符重载允许为用户定义的类型扩展运算符功能,但不能创建新运算符如`operator@`。重载的运算符必须至少有一个类类型参数,且不能改变内置类型运算符的含义。`.*::sizeof?`不可重载。赋值运算符`=`通常作为成员函数重载,确保封装性,如`Date`类的`operator==`。赋值运算符应返回引用并检查自我赋值。当未显式重载时,编译器提供默认实现,但这可能不足以处理资源管理。拷贝构造和赋值运算符在对象复制中有不同用途,需根据类需求定制实现。正确实现它们对避免数据错误和内存问题至关重要。接下来将探讨更多操作符重载和默认成员函数。
|
4月前
|
C++
c++进阶篇(一)——运算符重载
c++进阶篇(一)——运算符重载