C++?pimpl機(jī)制詳細(xì)講解
什么是PImpl機(jī)制
Pointer to implementation(PImpl ),通過(guò)將類(lèi)的實(shí)現(xiàn)細(xì)節(jié)放在一個(gè)單獨(dú)的類(lèi)中,從其對(duì)象表示中刪除它們,通過(guò)一個(gè)不透明的指針訪(fǎng)問(wèn)它們(cppreference 是這么說(shuō)的)
通過(guò)一個(gè)私有的成員指針,將指針?biāo)赶虻念?lèi)的內(nèi)部實(shí)現(xiàn)數(shù)據(jù)進(jìn)行隱藏
class Demo { public: ... private: DemoImp* imp_; }
為什么用PImpl 機(jī)制
個(gè)人拙見(jiàn)
- C++ 不像Java 后端型代碼,能有行業(yè)定式的列目錄名形成規(guī)范(controller、Dao等)
- 隱藏實(shí)現(xiàn),降低耦合性和分離接口(隱藏類(lèi)的具體實(shí)現(xiàn))
- 通過(guò)編譯期的封裝(隱藏實(shí)現(xiàn)類(lèi)的細(xì)節(jié))
業(yè)界實(shí)現(xiàn)
優(yōu)秀開(kāi)源代碼有實(shí)現(xiàn)
PImpl實(shí)現(xiàn)
方法一
cook_cuisine.h
#pragma once #include <unordered_map> #include <vector> #include <memory> // Pointer to impl ementation class CookImpl; // 后廚 class Cook { public: Cook(int, const std::vector<std::string>&); ~Cook(); std::vector<std::string> getMenu(); /* 獲取菜單 */ uint32_t getChefNum(); /* 獲取廚師數(shù)量 */ private: CookImpl* impl_; }; typedef std::shared_ptr<Cook> CookPtr; // 美妙的typedef 懶人工具
cook_cuisine.cc
#include "cook_cuisine.h" class CookImpl { public: CookImpl(uint32_t checf_num, const std::vector<std::string>& menu):checf_num_(checf_num), menu_(menu) {} std::vector<std::string> getMenu(); uint32_t getChefNum(); private: uint32_t checf_num_; std::vector<std::string> menu_; }; std::vector<std::string> CookImpl::getMenu() { return menu_; } uint32_t CookImpl::getChefNum() { return checf_num_; } Cook::Cook(int chef_num, const std::vector<std::string>& menu) { impl_ = new CookImpl(chef_num, menu); } Cook::~Cook() { delete impl_; } std::vector<std::string> Cook::getMenu() { return impl_->getMenu(); } uint32_t Cook::getChefNum() { return impl_->getChefNum(); }
方法二
cook_cuisine.h
#pragma once #include <unordered_map> #include <vector> #include <memory> #include "cook_cuisine_imp.h" // 后廚 class Cook { public: Cook(int, const std::vector<std::string>&); ~Cook(); std::vector<std::string> getMenu(); /* 獲取菜單 */ uint32_t getChefNum(); /* 獲取廚師數(shù)量 */ private: CookImplPtr impl_; }; typedef std::shared_ptr<Cook> CookPtr;
cook_cuisine.cc
#include "cook_cuisine.h" Cook::Cook(int chef_num, const std::vector<std::string>& menu) { impl_.reset(new CookImpl(chef_num, menu)); } Cook::~Cook() { } std::vector<std::string> Cook::getMenu() { return impl_->getMenu(); } uint32_t Cook::getChefNum() { return impl_->getChefNum(); }
cook_cuisine_imp.h
#pragma once #include <vector> #include <unordered_map> #include <memory> class CookImpl { public: CookImpl(uint32_t checf_num, const std::vector<std::string>& menu):checf_num_(checf_num), menu_(menu) {} std::vector<std::string> getMenu(); uint32_t getChefNum(); private: uint32_t checf_num_; std::vector<std::string> menu_; }; typedef std::shared_ptr<CookImpl> CookImplPtr;
cook_cusine_imp.cc
#include "cook_cuisine_imp.h" std::vector<std::string> CookImpl::getMenu() { return menu_; } uint32_t CookImpl::getChefNum() { return checf_num_; }
main.cc
#include "cook_cuisine.h" #include <iostream> using namespace std; // Testing, 平時(shí)開(kāi)發(fā)可千萬(wàn)別用這句 int main() { int checf_num = 10; const std::vector<std::string> menus = { "Chicken", "Beef", "Noodle", "Milk" }; CookPtr cook(new Cook(checf_num, menus)); auto cook_menu = cook->getMenu(); auto cook_checf_num = cook->getChefNum(); cout << "======================Chinese Cook======================\n"; cout << "============Checf: " << cook_checf_num << " people\n"; cout << "==========Menu\n"; for (size_t i = 0; i < cook_menu.size(); i++) { cout << "============" << i + 1 << " : " << cook_menu[i] << "\n"; } return 0; }
CMakeLists.txt
mkdir build
cd build
cmake ..
PImpl 缺點(diǎn)
空間開(kāi)銷(xiāo):每個(gè)類(lèi)都需要額外的指針內(nèi)存指向?qū)崿F(xiàn)類(lèi)
時(shí)間開(kāi)銷(xiāo):每個(gè)類(lèi)間接訪(fǎng)問(wèn)實(shí)現(xiàn)的時(shí)候多一個(gè)間接指針操作的開(kāi)銷(xiāo)
閱讀開(kāi)銷(xiāo):使用、閱讀和調(diào)試上帶來(lái)一些不便(不是啥問(wèn)題)
總結(jié)
每種設(shè)計(jì)方法都有它的優(yōu)點(diǎn)和缺點(diǎn)
PImpl 用一些內(nèi)存空間和額外類(lèi)的實(shí)現(xiàn)換取耦合性的下降,是可以接受的
但重點(diǎn)在:在性能/內(nèi)存要求不敏感處,PImpl 技術(shù)才更優(yōu)不錯(cuò)的發(fā)揮舞臺(tái)
極端例子:
你不可能在斐波那契的實(shí)現(xiàn)中還加個(gè)PImpl 機(jī)制,多此一舉
到此這篇關(guān)于C++ pimpl機(jī)制詳細(xì)講解的文章就介紹到這了,更多相關(guān)C++ pimpl機(jī)制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++語(yǔ)言中全局變量重復(fù)定義問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于C/C++語(yǔ)言中全局變量重復(fù)定義問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Qt中樹(shù)形控件Tree Widget的使用方法匯總
最近小編在研究Tree Widget樹(shù)形控件的相關(guān)知識(shí),這種控件其實(shí)有時(shí)還是很有用處的,我主要利用的是帶有復(fù)選框的樹(shù)形控件,下面通過(guò)實(shí)例代碼給大家介紹下Qt中樹(shù)形控件Tree Widget的一些使用方法,感興趣的朋友一起學(xué)習(xí)吧2021-11-11C語(yǔ)言數(shù)組實(shí)現(xiàn)掃雷游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)組實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06