I have a class that looks like the following:
class ModelCommand {
public:
virtual ~ModelCommand() {};
};
class FolderCommand : public ModelCommand {
public:
std::string text;
unsigned width;
bool isBackspace;
FolderCommand(bool isBackspace, unsigned width, std::string text = "") : text(text), width(width), isBackspace(isBackspace) {}
};
class CModel {
private:
string _folder;
public:
void Update(std::shared_ptr<ModelCommand> &cmd);
};
Then in my controller I have an instance of my model and update it using the new FolderCommand object I create:
shared_ptr<CModel> model;
shared_ptr<ModelCommand> cmd = dynamic_pointer_cast<ModelCommand>(make_shared<FolderCommand>(false, 20, "a"));
model->Update(cmd);
And then inside my update method of CModel I try to do the following:
void CModel::Update(std::shared_ptr<ModelCommand> &cmd) {
if (auto folderCmd = dynamic_pointer_cast<FolderCommand>(cmd)) {
if(!folderCmd->isBackspace)
// This is where _folder is unable to read memory
_folder += folderCmd->text;
else if(folderCmd->isBackspace && _folder.length() > 0)
_folder.erase(--_folder.end());
folderCmd->text = _folder;
}
}
This results in the CModel's _folder variable being "Unable to Read Memory".
Can someone explain and provide a solution to this problem?
Thanks.
UPDATE Added some more code for clarification
_foldermember to something, before appending to it?