首页 > 代码库 > boost库shared_ptr实现桥接模式

boost库shared_ptr实现桥接模式

主程序

/*将抽象部分与实现部分分离,使它们都可以独立的变化*/
#include "bridge.h"

int main()
{
	cout <<"main start" <<endl;	
	sample smp;
	smp.display();
	cout <<"main end" <<endl;	
	//sample::bridge bri;      //error: ‘class sample::bridge’ is private
	return 0;
}

bridge.h

#include <boost/smart_ptr.hpp>
#include <iostream>  
using namespace boost;  
using namespace std;  

class sample
{
	private:
		class bridge;
		shared_ptr<bridge> bptr;
	public:
		sample();
		void display();
};

class sample::bridge
{
	public:
		void display();
};

bridge.cpp

#include "bridge.h"
sample::sample()
{
		bptr = make_shared<bridge>();
}
void sample::display()
{
	bptr->display();
}
void sample::bridge::display()
{
	cout << "bridge display!"<<endl;
}

makefile

.SUFFIXES:.h .c .cpp .o

CC=$(CXX) $(CXX_FLAG)

RM = rm
SRCS = bridge.cpp main.cpp
PROGRAM = bridge
OBJS=$(SRCS:.cpp=.o)

INC_PATH =  -I$(BOOST_INCLUDE)
LIB_PATH =  -L$(BOOST_LIB)
LIBS = -lboost_date_time

$(PROGRAM):$(OBJS)
	$(CC) $? $(LIB_PATH) $(LIBS) -o $@

$(OBJS):$(SRCS)
	$(CC) $(CPPFLAGS) -c $(SRCS)  $(INC_PATH)
	
.PHONY:clean
clean:
	$(RM) $(PROGRAM) $(OBJS)
	
	


boost库shared_ptr实现桥接模式