溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++?pimpl機(jī)制怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2022-08-11 09:15:46 來源:億速云 閱讀:135 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“C++ pimpl機(jī)制怎么實(shí)現(xiàn)”,在日常操作中,相信很多人在C++ pimpl機(jī)制怎么實(shí)現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++ pimpl機(jī)制怎么實(shí)現(xiàn)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

什么是PImpl機(jī)制

Pointer to implementation(PImpl ),通過將類的實(shí)現(xiàn)細(xì)節(jié)放在一個(gè)單獨(dú)的類中,從其對象表示中刪除它們,通過一個(gè)不透明的指針訪問它們(cppreference 是這么說的)

通過一個(gè)私有的成員指針,將指針?biāo)赶虻念惖膬?nèi)部實(shí)現(xiàn)數(shù)據(jù)進(jìn)行隱藏

class Demo {
public:
	...
private:
	DemoImp* imp_;
}

為什么用PImpl 機(jī)制

個(gè)人拙見

  • C++ 不像Java 后端型代碼,能有行業(yè)定式的列目錄名形成規(guī)范(controller、Dao等)

  • 隱藏實(shí)現(xiàn),降低耦合性和分離接口(隱藏類的具體實(shí)現(xiàn))

  • 通過編譯期的封裝(隱藏實(shí)現(xiàn)類的細(xì)節(jié))

業(yè)界實(shí)現(xiàn)

優(yōu)秀開源代碼有實(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í)開發(fā)可千萬別用這句
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)

空間開銷:每個(gè)類都需要額外的指針內(nèi)存指向?qū)崿F(xiàn)類

時(shí)間開銷:每個(gè)類間接訪問實(shí)現(xiàn)的時(shí)候多一個(gè)間接指針操作的開銷

閱讀開銷:使用、閱讀和調(diào)試上帶來一些不便(不是啥問題)

到此,關(guān)于“C++ pimpl機(jī)制怎么實(shí)現(xiàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI