参考http://blog.csdn.net/calmreason/article/details/50909321
桥接模式 实现与抽象之间由指针关联
调用sample类 实际是调用sample类保存的实现类的方法
代码:
// 003.cpp: 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <memory> #include <iostream> using namespace std; class imp; class sample { public: sample(void); ~sample(void); void print(void) const; private: std::shared_ptr<imp> pimp_; }; sample::sample(void) :pimp_(new imp) { std::cout << "sample()" << std::endl; } sample::~sample(void) { std::cout << "~sample()" << std::endl; } class imp { public: imp(void); ~imp(void); void imp_print(void)const; }; imp::imp(void) { std::cout << "imp()" << std::endl; } imp::~imp() { std::cout <<"~imp()" << std::endl; } void imp::imp_print(void)const { std::cout << "imp::print()" << std::endl; } void sample::print(void)const { this->pimp_->imp_print(); } int main() { sample s; s.print(); return 0; }
时间: 2024-10-13 07:52:47