Hello to all, i have a smart pointer declare like this.
The class Human has several derived classes such as Admin, Staff and HR.Code:shared_ptr<Human> HumanPtr;
I have another class called commander has shared_ptr<Human> HumanPtr as member.
The class commander has a map lookup table the declare like
I wan to lookup the appropriate GenericCommandPtr based on the member HumanPtr and forward to the GenericCommandPtr derive class.Code:ypedef std::map<HumanPtr, GenericCommandPtr> commandCont;
My code:
Code:Commander commandPtr(thePtr); commandPtr.ForwardCommand(); class Commander { private: typedef boost::shared_ptr<GenericCommander> GenericCommandPtr; typedef boost::shared_ptr<Human> HumanPtr; typedef std::map<HumanPtr, GenericCommandPtr> commandCont; // Lookup table like vtable commandCont theCommander; HumanPtr theRealUserPtr; public: Commander(); explicit Commander(const HumanPtr&); ~Commander(); void RegisterCommanderInstance(); void ForwardCommand(); }; Commander::Commander() : theCommander(commandCont()), theRealUserPtr(HumanPtr()) { } // ========================================= Commander::Commander(const HumanPtr& userInstancePtr) : theCommander(commandCont()), theRealUserPtr(userInstancePtr) { } // ========================================= Commander::~Commander() { } // ========================================= void Commander::RegisterCommanderInstance() { static shared_ptr<Administrator> AdministratorPtr(new Administrator()); static shared_ptr<HumanResource> HumanResourcePtr(new HumanResource()); static shared_ptr<Staff> StaffPtr(new Staff()); static GenericCommandPtr AdministratorCommandPtr(new AdministratorCommander() ); static GenericCommandPtr HRCommandPtr(new HRCommander() ); static GenericCommandPtr StaffCommandPtr(new StaffCommander() ); theCommander.insert(commandCont::value_type(AdministratorPtr, AdministratorCommandPtr) ); theCommander.insert(commandCont::value_type(HumanResourcePtr, HRCommandPtr) ); theCommander.insert(commandCont::value_type(StaffPtr, StaffCommandPtr) ); } // ========================================= void Commander::ForwardCommand() { theCommander.find(theRealUserPtr)->second; }This code cause run time error map iterator not deference able.Code:void Commander::ForwardCommand() { theCommander.find(theRealUserPtr)->second; }
The purpose of this program is to look up the appropriate GenericCommandPtr class.
Please help.
It really urgent.
Thanks.




Reply With Quote