前言
此文章目前尚未整理完成,如需學習完整內容可參考隨附的 reference,或自行 google 搜尋
但因為作者要整理的筆記太多,如果想早點看到整理後的文章,可以下方留言「期待整理」之類的… 我會努力找時間優先整理!
pimpl 用途pimpl 是一種程式設計的方式,pimpl 拆開來就是 pointer to implementation 的縮寫,
正如同「pointer to implementation」的名字,
我們把實作(implementation)從 class 中拆開,
並在原先的 class 宣告一個 pointer 指向實作方式。
為什麼要這樣設計?首先我們要先把握一個概念「沒有一定最好的設計,只有最適合的設計 」,
意思就是如果沒有必要,不需要硬套用設計模式,
這很有可能發生我們常說的「over design」。
不論是「poor design」、「over design」都是不好的情況,需要自己判斷最適當的使用。
pimpl 是把 class 本身的一些實作直接拆出來的方法,在程式架構還不夠複雜的時候基本沒有必要使用。
隱藏實作 (接口與實作的分離)我們觀察下方的程式碼,因為我們把「原 MyClass foo() 實作部分」改成「用 impl 去拿位於 MyClassimpl 的 foo()」。
如果我們今天是開 API 給其他使用者,其他使用者只會看到 MyClass 的 foo(),而我們可以把 MyClassimpl 的 foo() 給隱藏起來。
而我們真正的實作都在 MyClassimpl 的 foo()
void foo(){
return impl -> foo();
};
降低耦合、更少的編譯時間目前有點看不懂,先放著,改天想通再回來寫
範例程式碼#include <iostream>
using namespace std;
class MyClassimpl{
public:
void foo(){
cout << "foo in MyClassimpl" << endl;
};
};
class MyClass{
public:
void foo(){
cout << "foo in MyClass" << endl;
return impl -> foo();
};
private:
// MyClassimpl* impl;
typedef unique_ptr<MyClassimpl> MyClassimplPtr;
MyClassimplPtr impl;
};
int main(){
MyClass obj;
obj.foo();
return 0;
}
Reference⭐C++ 基礎用法 相關文章整理⭐: 1. 【C++】C++ compile 程式碼 使用 c++ 11 與使用相關的 package 2. 【C++】C/C++ 顯示資料的類別 (type) sample code (內含範例程式碼) print C data type, cout C++ data type, get variable type in c++ 3. 【C++】C++ 複製 2D array的方法 copy 2d array memcpy sample code (內含範例程式碼) ⭐Modern C++ ⭐: ⭐C++ 字串處理相關文章整理⭐: 1. 【C++】字串 char string stringstream 相關用法總整理 (內含範例程式碼) 與利用 sprinf, snprinf assign 值的方法 2. 【C++】字串 char string stringstream 「轉換」用法總整理 (內含範例程式碼) 3. 【C】printf, fprintf, sprintf, snprintf 相關用法總整理 (內含範例程式碼) 4. 【C++】C++ String 用法 連接兩個 String c++ string concat ⭐C++ 系統偵測相關文章整理⭐: 1. 【C++】C++ 利用 dirent.h 計算資料夾的檔案數量 count files sample code (內含範例程式碼) 2. 【C++】C++ inotify sample code 偵測指定路徑底下的文件變化 (內有範例程式碼) ⭐C++ OpenCV 相關文章整理⭐: 1. 【OpenCV】c++ OpenCV – 在 ubuntu 上第一次執行 OpenCV 程式 sample code (內含範例程式碼) 2. 【OpenCV】c++ OpenCV - 在圖片上寫上文字 cv::putText sample code (內含範例程式碼) 3. 【OpenCV】c++ OpenCV - cv::Rect 矩形用法與相關功能函數 sample code (內含範例程式碼) 4. 【OpenCV】c++ OpenCV - OpenCV 中的純量 定義顏色 cv::Scalar(255,255,255) color sample code (內含範例程式碼) 5. 【OpenCV】用 C++ 計算 iou 的方法 與網路算法常見錯誤(內附範例程式碼) sample code ⭐C++ Makefile 相關文章整理⭐: 1. 【C++ Makefile】- 1 / 嘗試撰寫自己的第一份 Makefile 2. 【C++ Makefile】- 2 / 新增自己的變數 3. 【C++ Makefile】- 3 / Makefile 常用變數 -「$@」, 「$^」 4. 【C++ Makefile】- 4 / Makefile 常用 fake target -「.PHONY」
5. 【C++ Makefile】- 5 / Makefile 內建變數 -「$@」, 「$^」, 「$<」, 「$* 」,「$? 」
⭐【喜歡我的文章嗎? 歡迎幫我按讚~ 讓基金會請創作者喝一杯咖啡! 】
如果喜歡我的文章,請幫我在下方【按五下Like 】 (Google, Facebook 免註冊 ),會由 「LikeCoin 」 贊助作者鼓勵繼續創作,讀者們「只需幫忙按讚,完全不用出錢 」哦!