// base_from_member.cpp // made by davidsu33 // 2014-9-28 #include "stdafx.h" #include <boost/utility/base_from_member.hpp> #include <boost/core/addressof.hpp> #include <string> typedef int RGBA; using namespace std; //手 class BaseHand { public: BaseHand(RGBA color) :m_rgba(color) { } virtual ~BaseHand(){} private: RGBA m_rgba; }; //脚 class BaseFoot { public: BaseFoot(int size, const std::string& style) :m_size(size), m_style(style){}; virtual ~BaseFoot(){} private: int m_size; std::string m_style; }; //头 class BaseHead { public: BaseHead(int width, int height): m_width(width),m_height(height){}; virtual ~BaseHead(){} private: int m_width; int m_height; }; //NPC class GameNPC : public BaseHand, public BaseFoot, public BaseHead { public: GameNPC(RGBA rgba, int footsize, const std::string& footstyle, int headWidth, int headHeight): BaseHand(rgba),BaseFoot(footsize, footstyle),BaseHead(headWidth, headHeight){} }; //base_from_member class BFMGameNPC :public boost::base_from_member<BaseHand>, boost::base_from_member<BaseFoot>, boost::base_from_member<BaseHead> { public: typedef boost::base_from_member<BaseHand> pbase0_type; typedef boost::base_from_member<BaseFoot> pbase1_type; typedef boost::base_from_member<BaseHead> pbase2_type; public: BFMGameNPC(); ~BFMGameNPC(); }; BFMGameNPC::BFMGameNPC() //简化多参数的继承 :pbase0_type(1002), pbase1_type(2, "pegine"), pbase2_type(100, 200) { } BFMGameNPC::~BFMGameNPC() { } int _tmain(int argc, _TCHAR* argv[]) { BFMGameNPC bfmnp; GameNPC npc(10002, 2, "pengine", 100, 200); return 0; }