網站首頁 編程語言 正文
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;
};
#endifDuree.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;
}
編譯執行

總結
原文鏈接:https://blog.csdn.net/jiuweideqixu/article/details/106780533
相關推薦
- 2022-06-22 android使用intent傳遞參數實現乘法計算_Android
- 2023-04-18 關于C#中async/await的用法實例詳解_C#教程
- 2022-07-26 Golang異常處理之defer,panic,recover的使用詳解_Golang
- 2022-01-12 解決element-ui 日期選擇器提交后臺數據不準確問題
- 2022-07-20 C語言通過三步翻轉法實現單詞倒置詳解_C 語言
- 2022-11-07 PostgreSQL游標與索引選擇實例詳細介紹_PostgreSQL
- 2023-02-14 C++深入分析數據在內存中的存儲形態_C 語言
- 2023-04-06 python判斷列表為空的三種方法總結_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支
