String removed after constructor initialize on if statement on C++ -


i'm experiencing extrange behaviour on c++ (mvs 2010) when initializing on constructor class called managerenderlistenercommand. implemented command design patter, managerenderlistenercommand command 1 of concrete commands.

the place call managerenderlistenercommand

void mediator::change(negotiator* negotiator, negotiatorevent& negotiatorevent){     icommand* command = null;     if(negotiatorevent.matchevent("addtoviewport")){         command = static_cast<icommand*> (&addtoviewportcommand(mcameramanager, mscenecreator, mengine));     }else if (negotiatorevent.matchevent("managerenderlistener")){         command = static_cast<icommand*> (&managerenderlistenercommand(mobserverregistry, mengine, negotiatorevent.getmessage()));     }      //execute created command     if (command) command->execute(); } 

as can see in code, managerenderlistener receives string, in case string contains word add contained on negotiatorevent class (negotiatorevent.getmessage()).

the problem is, on constructor, take string on private member, debugging can see after assgnation , casting removed , reinitialized "". have tried static_cast, dynamic_cast. give clue, think it's visibility problem don't know how manage it.

}else if (negotiatorevent.matchevent("managerenderlistener")){     //here mmessage = ""     command = static_cast<icommand*> (&managerenderlistenercommand(mobserverregistry, mengine, negotiatorevent.getmessage()));     //here mmessage again "" instead of add } 

managerenderlistener.cpp

#include "managerenderlistenercommand.h"  managerenderlistenercommand::managerenderlistenercommand(     ogrerenderobserverregistry* observerregistry,      ogreengine* engine,     string message):         mobserverregistry(observerregistry),         mengine(engine),         mmessage(message){ }  void managerenderlistenercommand::execute(){     if (mmessage.compare("add") == 0){         mengine->addrenderlistener(mobserverregistry->getcachedobserver());     }else if (mmessage.compare("detach") == 0){         mengine->detachrenderlistener(mobserverregistry->getcachedobserver());     } } 

if need more details ask it. help.

yo're using dangling pointer. object construction you're using creates temporary object, destroyed when full expression (the static_cast) ends. still have pointer (in command), object has been destroyed.

you need create command in way have persist until in call execute(). if code you've shown, this:

if(negotiatorevent.matchevent("addtoviewport")){     addtoviewportcommand(mcameramanager, mscenecreator, mengine).execute(); }else if (negotiatorevent.matchevent("managerenderlistener")){     managerenderlistenercommand(mobserverregistry, mengine, negotiatorevent.getmessage()).execute(); } 

if there more steps in between creation , call execute(), have create command dynamically:

void mediator::change(negotiator* negotiator, negotiatorevent& negotiatorevent){     icommand* command = null;     if(negotiatorevent.matchevent("addtoviewport")){         command = new addtoviewportcommand(mcameramanager, mscenecreator, mengine);     }else if (negotiatorevent.matchevent("managerenderlistener")){         command = new managerenderlistenercommand(mobserverregistry, mengine, negotiatorevent.getmessage());     }      //execute created command     if (command) command->execute();      delete command; } 

if have access c++11, use std::unique_ptr<icommand> command instead of raw pointer.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -