CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 22

Threaded View

  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Thumbs up Smart Pointer Map Lookup

    Hello to all, i have a smart pointer declare like this.

    Code:
    shared_ptr<Human> HumanPtr;
    The class Human has several derived classes such as Admin, Staff and HR.

    I have another class called commander has shared_ptr<Human> HumanPtr as member.
    The class commander has a map lookup table the declare like

    Code:
    ypedef std::map<HumanPtr, GenericCommandPtr> commandCont;
    I wan to lookup the appropriate GenericCommandPtr based on the member HumanPtr and forward to the GenericCommandPtr derive class.

    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; 
    }
    Code:
    void Commander::ForwardCommand()
    {
    	theCommander.find(theRealUserPtr)->second; 
    }
    This code cause run time error map iterator not deference able.

    The purpose of this program is to look up the appropriate GenericCommandPtr class.

    Please help.

    It really urgent.

    Thanks.
    Last edited by Peter_APIIT; July 31st, 2009 at 03:23 AM.
    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured