網站首頁 編程語言 正文
一、類和對象、this指針
OOP語言的四大特征是什么?
- 抽象
- 封裝、隱藏
- 繼承
- 多態
類體內實現的方法會自動處理為inline函數。
類對象的內存大小之和成員變量有關
類在內存上需要對齊,是為了減輕cup在內存上的io次數
查看類對象的大小的指令:cl className.cpp /d1reportSingleClassLayout類名
一個類可以定義無數個對象,每個對象都有自己的成員變量,但是他們共享一套成員方法。
有一個問題:Q1:類中的成員方法是怎么知道要處理哪個對象的信息的?
A1:在調用成員方法的時候會在參數列表里隱式的給定對象內存的地址。如下所示:
類的成員方法一經編譯,所有方法參數都會加一個this指針,接收調用該方法的對象的地址,即下圖中的CGoods *this
二、掌握構造函數和析構函數
定義一個SeqStack類:
class SeqStack { public: SeqStack(int size = 10) :_top(-1), _size(size) { _pstack = new int[size]; } ~SeqStack() { cout << this << "~SeqStack()" << endl; delete[] _pstack; _pstack = nullptr; } void push(int val) { if (full()) { resize(); } _pstack[++_top] = val; } void pop() { if (empty()) { return; } --_top; } int top() { return _pstack[_top]; } bool empty() { return _top == -1; } bool full() { return _top == _size-1; } private: int* _pstack; int _top; int _size; void resize() { int* ptmp = new int[_size * 2]; for (int i = 0; i < _size; i++) { ptmp[i] = _pstack[i]; } delete[] _pstack; _pstack = ptmp; _size *= 2; } }; /** 運行過程 */ int main() { SeqStack sq1; for (int i = 0; i < 15; i++) { sq1.push(rand() % 100); } while (!sq1.empty()) { cout << sq1.top() << " "; sq1.pop(); } return 0; }
三、掌握對象的深拷貝和淺拷貝
.data段的對象是程序啟動的時候構造的,程序結束的時候析構的
heap堆上對象是new的時候構造的,delete的時候析構的
stack棧上的對象是在調用函數的時候構造的,執行完函數時析構的
如果對象占用外部資源,淺拷貝就會出現問題:會導致一個對象指向的內存釋放,從而造成另一個對象中的指針成為野指針。所以就要對這樣的對象進行深拷貝,在新的對象中重新開辟一塊空間,使兩者互不干涉。
注意:在面向對象中,要避免使用memcpy進行拷貝,因為對象的內存占用不確定,會因為對象中保存指針而造成淺拷貝。需要拷貝的時候只能用for循環逐一拷貝。
深拷貝:
SeqStack& operator=(const SeqStack& src) { cout << "operator=" << endl; //防止自賦值 if (this == &src) { return *this; } delete[] _pstack;//需要釋放掉自身占用的外部資源 _pstack = new int[src._size]; for (int i = 0; i <= src._top; i++) { _pstack[i] = src._pstack[i]; } _top = src._top; _size = src._size; return *this; } SeqStack(const SeqStack& src) { cout << this << "SeqStack(const SeqStack& src)" << endl; _pstack = new int[src._size]; for (int i = 0; i <= src._top; i++) { _pstack[i] = src._pstack[i]; } _top = src._top; _size = src._size; }
四、類和對象應用實踐
類Queue:
#pragma once class CirQueue { public: CirQueue(int size = 10) { _pQue = new int[size]; _front = _rear = 0; _size = size; } CirQueue(const CirQueue& src) { _size = src._size; _front = src._front; _rear = src._rear; _pQue = new int[_size]; for (int i = _front; i != _rear; i = (i + 1) % _size) { _pQue[i] = src._pQue[i]; } } ~CirQueue() { delete[] _pQue; _pQue = nullptr; } CirQueue& operator=(const CirQueue& src) { if (this == &src) { return *this; } delete[] _pQue;//需要釋放掉自身占用的外部資源 _size = src._size; _front = src._front; _rear = src._rear; _pQue = new int[_size]; for (int i = _front; i != _rear; i = (i + 1) % _size) { _pQue[i++] = src._pQue[i]; } return *this; } void push(int val) { if (full()) { resize(); } _pQue[_rear] = val; _rear = (_rear + 1) % _size; } void pop() { if (empty()) { return; } _front = (_front + 1) % _size; } int front() { return _pQue[_front]; } bool full() { return (_rear + 1) % _size == _front; } bool empty () { return _front == _rear; } private: int* _pQue; int _front; int _rear; int _size; void resize() { int* ptmp = new int[_size * 2]; int index = 0; for (int i = _front; i != _rear; i=(i+1)%_size) { ptmp[index++] = _pQue[i]; } delete[] _pQue; _pQue = ptmp; _front = 0; _rear = index; _size *= 2; } };
類String:
#pragma once #include <algorithm> class String { public: String(const char* str = nullptr) { if (str != nullptr) { _pChar = new char[strlen(str) + 1]; strcpy(_pChar, str); } else { _pChar = new char[1]; *_pChar = '\0'; } } String(const String& str) { _pChar = new char[strlen(str._pChar)+1]; strcpy(_pChar, str._pChar); } ~String() { delete[] _pChar; _pChar = nullptr; } String& operator=(const String& str) { if (this == &str) { return *this; } delete[] _pChar;//需要釋放掉自身占用的外部資源 _pChar = new char[strlen(str._pChar) + 1]; strcpy(_pChar, str._pChar); return *this; } private: char* _pChar; };
五、掌握構造函數的初始化列表
初始化列表和寫在構造體里有什么區別:
初始化列表會直接定義并且賦值;放在構造體里會先執行定義操作,在對定義好的對象賦值。
對象變量是按照定義的順序賦值的,與構造函數中初始化列表的順序無關。上圖中的ma是0xCCCCCCCC,mb是10,ma未賦值。
六、掌握類的各種成員方法及其區別
普通成員方法和常成員方法,是可以重載的,常成員方法可以在對象聲明為const的時候調用。
對象聲明為const的時候,調用成員方法是通過const對象的指針調用的,而普通的成員方法默認生成的是普通的指針對象,不能直接賦值。
只要是只讀操作的成員方法,一律實現成const常成員方法
三種成員方法:
七、指向類成員的指針
class Test { public: void func() { cout << "call Test::func" << endl; } static void static_func() { cout << "call Test::static_func" << endl; } int ma; static int mb; }; int Test::mb=0; int main() { Test t1; Test *t2 = new Test();//在堆上生成對象,并用指針指向 //使用指針調用類成員方法(前面要加類的作用域Test::) void (Test:: * pfunc)() = &Test::func; (t1.*pfunc)(); (t2->*pfunc)(); //定義指向static的類成員方法 void(*pfunc1)() = &Test::static_func; (*pfunc1)(); //使用指針指向類成員變量,前面要加類的作用域Test:: int Test::* p = &Test::ma; t1.*p = 20; cout << t1.*p << endl; t2->*p = 30; cout << t2->*p << endl; int* p1 = &Test::mb; *p1 = 40; cout << *p1 << endl; delete t2; return 0; }
輸出為:
總結
原文鏈接:https://www.cnblogs.com/woden3702/p/16273541.html
相關推薦
- 2022-12-21 Kotlin?launch原理全面分析_Android
- 2022-08-28 ERROR 1366 (HY000): Incorrect string value: ‘\xE8\
- 2023-01-26 如何在.Net?7中將Query綁定到數組詳解_實用技巧
- 2022-04-17 sa-token快速添加多鑒權體系
- 2023-06-21 Android面向單Activity開發示例解析_Android
- 2022-06-06 CSS3動畫:wifi 焦點擴散漸變
- 2022-09-10 Python并發編程多進程,多線程及GIL全局解釋器鎖_python
- 2022-09-26 數據庫多表聯查的方式
- 最近更新
-
- 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同步修改后的遠程分支