C++中如何将operator==定义为类的成员函数

 更新时间:2023年01月28日 09:09:26   作者:久许  
这篇文章主要介绍了C++中如何将operator==定义为类的成员函数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

C++将operator==定义为类的成员函数

Duree.h

#ifndef _DUREE_H
#define _DUREE_H
class Duree{
    public:
    Duree(int heures = 0,int minutes = 0,int secondes = 0);
    bool estEgal(Duree const &b)const;
    bool operator==(Duree const& other);//如果把operator==作为类的成员函数,那么只需要一个参数就够了
    private:
    int m_heures;
    int m_minutes;
    int m_secondes;

};
#endif

Duree.cpp

#include<iostream>
#include "Duree.h"
using namespace std;
Duree::Duree(int heures,int minutes,int secondes):m_heures(heures),m_minutes(minutes),m_secondes(secondes){
    cout<<m_heures<<" "<<m_minutes<<" "<<m_secondes<<endl;
}
bool Duree::estEgal(Duree const& other)const{
    return (m_heures==other.m_heures && m_minutes == other.m_minutes && m_secondes == other.m_secondes);
}

bool Duree::operator==(Duree const& other){
    return estEgal(other);
}

Duree_Main.cpp

#include<iostream>
#include "Duree.h"
using namespace std;

int main(){
    Duree first(10,10,10),second(15,20);
    if(first == second)
        cout<< "Les durees sont identiques"<<endl;
    else
        cout<< "Les durees sont differents"<<endl;

    return 0;
}

编译运行

参考

https://stackoverflow.com/questions/23162870/c-bool-operator

C++对operator=进行重写

/*************************************************************************
	> File Name: copy1.cpp
	> Author: 
	> Mail: 
	> Created Time: 2020年06月17日 星期三 15时36分57秒
 ************************************************************************/

#include<iostream>
using namespace std;

class Example{
    std::string* ptr;
    public:
    Example():ptr(new std::string){}//new std::string返回的值是存储字符串的地址
    Example(const std::string& str):ptr(new std::string(str)){}
    Example(const Example& example):ptr(new std::string(example.content())){};
    ~Example(){delete ptr;}
    const std::string& content()const{return *ptr;}
    Example& operator= (const Example& x){
        cout<<"执行了"<<endl;
        delete ptr;//释放指针指向的字符串占用的内存
        ptr = new string(x.content());
        return *this;
    }
    Example operator+(const Example & rhs){
        return Example(content()+rhs.content());
    }

    void print_information()const{
        cout<< *ptr<<endl;
    }
};

int main(){
    Example bar1("hello");
    bar1.print_information();
    Example bar2("world");
    bar2.print_information();
    Example bar3 = Example(bar1);
    bar3.print_information();
    Example bar4 = bar2;
    bar4.print_information();

    Example foo("Exam");
    Example ba = Example("ple");
    foo = foo + ba;
    cout<<"foo's content:"<<foo.content()<<endl;


    
    return 0;
}

编译执行

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • C语言 一级指针与二级指针详细介绍

    C语言 一级指针与二级指针详细介绍

    这篇文章主要介绍了C语言 一级指针与二级指针详细介绍的相关资料,需要的朋友可以参考下
    2016-10-10
  • c++ string的erase删除方法

    c++ string的erase删除方法

    这篇文章主要介绍了c++ string的erase删除方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 使用c++11 constexpr时遇到的坑详解

    使用c++11 constexpr时遇到的坑详解

    c++11 constexpr将变量声明为constexpr类型以便由编译器来验证变量是否是一个常量表达式,这篇文章主要给大家介绍了关于使用c++11 constexpr时遇到的坑,需要的朋友可以参考下
    2021-05-05
  • C++实现停车场管理系统

    C++实现停车场管理系统

    这篇文章主要为大家详细介绍了C++实现停车场管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • C++ 常量成员函数学习笔记

    C++ 常量成员函数学习笔记

    这篇文章主要为大家介绍了C++ 常量成员函数学习笔记,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • C/C++语言中结构体的内存分配小例子

    C/C++语言中结构体的内存分配小例子

    当未用 #pragma 指令指定编译器的对齐位数时,结构体按最长宽度的数据成员的宽度对齐;当使用了 #pragma 指令指定编译器的对齐位数时,结构体按最长宽度的数据成员的宽度和 #pragma 指令指定的位数中的较小值对齐
    2013-10-10
  • 全面了解C语言 static 关键字

    全面了解C语言 static 关键字

    这篇文章主要介绍了全面了解C语言 static 关键字,文章首先通过先介绍一下头文件的创建展开主题的详细内容,需要的小伙伴可以参考一下
    2022-04-04
  • C语言超详细讲解递归算法汉诺塔

    C语言超详细讲解递归算法汉诺塔

    汉诺塔问题是一个经典的问题。汉诺塔(Hanoi Tower),又称河内塔,源于印度一个古老传说。本文将用Java求解这一问题,感兴趣的可以学习一下
    2022-05-05
  • C++11, 14, 17对tuple元素的访问详情

    C++11, 14, 17对tuple元素的访问详情

    这篇文章主要介绍了C++11, 14, 17对tuple元素的访问详情,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • C++实现简单学生信息管理系统

    C++实现简单学生信息管理系统

    这篇文章主要为大家详细介绍了C++实现简单学生信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03

最新评论