網站首頁 編程語言 正文
一.const修飾類的成員函數
1.問題引出:
給出一段簡單的代碼
代碼段:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class Date1
{
public:
Date1(int year = 2000) 類的全缺省構造函數(可無參調用)
{
_year = year;
}
void Prin()
{
cout << "Print Date:" << _year << endl;
}
private:
int _year;
};
int main()
{
const Date1 a; 定義一個const修飾的對象a(該對象只可讀,不可被寫入)
a.Prin();
return 0;
}
該段程序會編譯報錯:
2.問題分析
上述代碼段出錯的原因要從類的成員函數的隱含參數this指針出發進行分析:
注意:
- 由于a是const修飾的對象,因此&a 取出的是 const Date *類型的指針,該指針只可對a對象的內存空間進行讀取操作而不可進行寫入操作(該指針的權限為只可讀取不可寫入)。
- Prin函數的形參是Date * const this指針,該類型指針同時具有讀取和寫入內存空間的權限。
- 將&a賦給Prin的形參this,就會使指針的讀寫權限被放大,因此編譯無法通過(指針是靈活而危險的存在,編譯器只允許其讀寫權限被縮小而不允許其權限被放大)
3.const修飾類的成員函數?
我們知道類的每個成員函數都有一個隱含的this指針形參(類型為:類名*const this)。
為了使被const修飾的對象(比如是上面代碼段中的a)可以調用其成員對象,C++規定可以用const來修飾類的成員函數。
類中被const修飾的“成員函數”稱為const成員函數,const修飾類成員函數,本質上修飾該成員函數隱含的this指針,表明在該成員函數中不能對類的任何成員變量進行修改。(修飾后成員函數的this指針形參類型變為:const 類名* const this)
比如:
const修飾的對象不可以調用非const修飾的成員函數(類指針傳參給this指針時讀寫權限被放大):
非const修飾的對象可以調用const修飾的成員函數(類指針傳參給this指針時讀寫權限被縮小):
const修飾的成員函數內不可以調用其它的非const修飾的成員函數(this指針之間傳參時讀寫權限被放大):
非const修飾的成員函數內可以調用其它的const修飾的成員函數(this指針之間傳參時讀寫權限被縮小):
當類的成員函數中沒有對類的成員變量進行任何形式的修改操作時,該成員函數最好都用const來修飾(這樣安全同時又使得const修飾的對象可以調用該成員函數)以保證代碼的健壯性。
二. 類的兩個默認的&運算符重載
編譯器會默認生成兩個類的&(取地址)重載用于類的取地址操作(如果我們自定義了類的取地址重載則編譯器便不會再生成默認的)
C++中,內置運算符若要直接作用于類對象則必須經過重載。
若想取到類對象的地址,我們可以對&運算符進行重載,比如:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class Date1
{
public:
Date1(int year = 2000)
{
_year = year;
}
Date1* operator& () 對&進行重載用于非const修飾的對象的取地址
{
return this;
}
const Date1* operator&() const 對&進行重載用于const修飾的對象的取地址
{
return this;
}
private:
int _year;
};
int main()
{
const Date1 a; 定義一個const修飾的對象a(該對象只可讀,不可被寫入)
Date1 b;
cout << &a << endl;
cout << &b << endl;
return 0;
}
這兩個默認成員函數一般不用重新自定義 ,編譯器默認會生成,編譯其默認生成的&重載和上面我們自定義的成員函數應該沒有什么區別(至少功能上沒區別)。
三. 日期類小練習?
日期類頭文件:
為了提高代碼的可維護性和可讀性,將日期類的成員函數的聲明和定義分開寫。
#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//記錄日期的類
class Date
{
public:
//Date的構造函數
Date(int day=1, int month=1, int year=1);
//獲取月份天數的方法
int GetMonthday(const int month) const;
//類對象的日期打印函數
void Print() const;
//判斷某個日期是星期幾,并打印出來
void GetWeekDay() const ;
//一組比較運算符的重載
bool operator> (const Date& date)const;
bool operator==(const Date& date)const;
//在邏輯上我們只需定義出大于(或小于)和等于的判斷函數,剩余的判斷函數我們就可以通過復用的方
式簡化代碼書寫
bool operator<(const Date& date)const;
bool operator>=(const Date& date)const;
bool operator<=(const Date& date)const;
bool operator!=(const Date& date)const;
//一組日期+(-)整數的操作和+=(-=)整數的操作
Date operator+(const int day)const;
Date& operator+=(const int day);
Date operator-(const int day)const;
Date& operator-=(const int day);
Date& operator=(const Date& date);
//一組前置++(--)和后置++(--)的重載
Date& operator++(); //實現日期類的前置++
Date operator++(int); //實現日期類的后置++
Date& operator--(); //實現日期類的前置--
Date operator--(int); //實現日期類的后置--
//實現時期相減的操作符重載
int operator-(const Date& date)const;
private:
int _day;
int _month;
int _year;
};
日期類的成員函數的實現:
#include "Date.h"
//Date的構造函數
Date ::Date(int day, int month, int year)
{
_day = day;
_month = month;
_year = year;
if (_year <= 0 || _month <= 0 || _month > 12 || _day <= 0 || _day > GetMonthday(_month))
{
cout << "date invalued please exit the app" << endl;
exit(0);
}
}
//獲取相應月份天數的方法
int Date::GetMonthday(const int month)const
{
static const int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int ret = arr[month - 1];
if (((0 == _year % 4 && 0 != _year % 100) || (0 == _year % 400)) && 2 == month)
{
ret++;
}
return ret;
}
//類對象的日期打印函數
void Date::Print()const
{
cout << _year << ' ' << _month << ' ' << _day << ' ' << endl;
}
//判斷某個日期是星期幾,并打印出來
//注意this指針不能由用戶去傳
void Date::GetWeekDay()const
{
const char* arr[7] = { "星期一","星期二","星期三","星期四","星期五","星期六","星期日" };
const Date tem(1, 1, 1900);
const int subret = (*this)-tem;
printf("%s\n", arr[(subret % 7)]);
}
//將 > 運算符進行重載
bool Date ::operator> (const Date& date)const
{
if (_year > date._year)
{
return true;
}
else if (_year == date._year && _month > date._month)
{
return true;
}
else if (_year == date._year && _month == date._month && _day > date._day)
{
return true;
}
return false;
}
//將 =運算符進行重載
bool Date:: operator==(const Date& date)const
{
if (date._day == _day && date._month == _month && date._year == _year)
{
return true;
}
return false;
}
//在邏輯上我們只需定義出大于(或小于)和等于的判斷函數,剩余的判斷函數我們就可以通過復用的方式簡化代碼書寫
bool Date :: operator>= (const Date& date)const
{
if ((*this) > date || (*this) == date)
{
return true;
}
return false;
}
bool Date :: operator < (const Date& date)const
{
if ((*this) >= date)
{
return false;
}
return true;
}
bool Date :: operator<=(const Date& date)const
{
if ((*this) > date)
{
return false;
}
return true;
}
bool Date:: operator!= (const Date& date)const
{
if ((*this) == date)
{
return false;
}
return true;
}
//一組日期+(-)整數的操作和+=(-=)整數的操作
Date& Date::operator+=(const int day)
{
if (day < 0)
{
(*this) -= (-day);
return (*this);
}
_day += day;
while (_day > GetMonthday(_month))
{
if (_month < 12)
{
_day -= GetMonthday(_month);
_month++;
}
else
{
_day -= GetMonthday(_month);
_year++;
_month = 1;
}
}
return (*this);
}
Date Date::operator+(const int day)const
{
Date tem(*this);
tem += day;
return tem;
}
Date& Date::operator-=(const int day)
{
if (day < 0)
{
(*this) += (-day);
return (*this);
}
_day -= day;
while (_day <= 0 )
{
if (_month > 1)
{
_month--;
_day += GetMonthday(_month);
}
else
{
_year--;
_month = 12;
_day += GetMonthday(_month);
}
}
if (_year <= 0)
{
cout << "operation invalued" << endl;
exit(0);
}
return (*this);
}
Date Date::operator-(int day)const
{
Date tem(*this);
tem -= (day);
return tem;
}
Date& Date ::operator=(const Date& date)
{
if (this != &date)
{
_day = date._day;
_month = date._month;
_year = date._year;
}
return (*this);
}
//一組前置++(--)和后置++(--)的重載
Date& Date ::operator++() //實現日期類的前置++
{
(*this) += 1;
return (*this);
}
Date Date ::operator++(int) //實現日期類的后置++
{
Date tem(*this);
(*this) += 1;
return tem;
}
Date& Date:: operator--() //實現日期類的前置--
{
(*this) -= 1;
return (*this);
}
Date Date:: operator--(int) //實現日期類的后置--
{
Date tem(*this);
(*this) -= 1;
return tem;
}
//實現時期相減的操作符重載
int Date::operator-(const Date& date)const
{
int count = 0;
Date min;
if ((*this) < date)
{
min = (*this);
while (min != date)
{
min++;
count++;
}
return -count;
}
else
{
min = date;
while (min != (*this))
{
min++;
count++;
}
return count;
}
}
總結
原文鏈接:https://blog.csdn.net/weixin_73470348/article/details/128761564
相關推薦
- 2022-10-05 Numpy中Meshgrid函數基本用法及2種應用場景_python
- 2022-04-25 .net5?使用Hangfire的過程記錄_實用技巧
- 2022-04-26 Swift踩坑實戰之一個字符引發的Crash_Swift
- 2022-07-08 Python設置Word全局樣式和文本樣式的示例代碼_python
- 2022-04-04 微信小程序:返回上一頁,刷新頁面內容
- 2022-06-17 Go實現線程池(工作池)的兩種方式實例詳解_Golang
- 2024-02-27 credentials to a set of origins, list them explici
- 2022-08-26 C++中Boost的智能指針scoped_ptr_C 語言
- 最近更新
-
- 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同步修改后的遠程分支