您好,登录后才能下订单哦!
这篇文章主要介绍“C++ pimpl机制怎么实现”,在日常操作中,相信很多人在C++ pimpl机制怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++ pimpl机制怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Pointer to implementation(PImpl ),通过将类的实现细节放在一个单独的类中,从其对象表示中删除它们,通过一个不透明的指针访问它们(cppreference 是这么说的)
通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏
class Demo { public: ... private: DemoImp* imp_; }
个人拙见
C++ 不像Java 后端型代码,能有行业定式的列目录名形成规范(controller、Dao等)
隐藏实现,降低耦合性和分离接口(隐藏类的具体实现)
通过编译期的封装(隐藏实现类的细节)
业界实现
优秀开源代码有实现
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(); /* 获取厨师数量 */ 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(); /* 获取厨师数量 */ 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, 平时开发可千万别用这句 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 ..
空间开销:每个类都需要额外的指针内存指向实现类
时间开销:每个类间接访问实现的时候多一个间接指针操作的开销
阅读开销:使用、阅读和调试上带来一些不便(不是啥问题)
到此,关于“C++ pimpl机制怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。