Skip to content

Instantly share code, notes, and snippets.

@travisperson
Created March 2, 2012 18:49
Show Gist options
  • Save travisperson/1960338 to your computer and use it in GitHub Desktop.
Save travisperson/1960338 to your computer and use it in GitHub Desktop.
Interesting Visual Studio Effect
// Given the following definitions:
void
BankMenu::execute(Command cmd)
{
// Runs a command
//...
}
// Given the following definitions:
// Constructor for the Command class
Command::Command(string command)
{
// Parses the string into a command structure
this->parse(command);
}
// Just for your reference:
// Loads the simulation commands into the command_ list
Simulator::load(string file)
{
std::ifstream input;
std::string line;
input.open(file.c_str());
if ( input.is_open() )
{
while ( input.good() )
{
std::getline(input, line);
this->commands_.insert_back(line);
}
input.close();
}
else std::cout << "Could not open file for reading." << std::endl;
}
// In visual studio this works:
// Note: List<std::string> commands_;
void
Simulator::run ()
{
// this->bm_ is a BankMenu object.
List<std::string>::iterator it = this->commands_.begin();
std::cout << *it << std::endl;
for(; it != this->commands_.end(); it++)
{
std::cout << " > " << *it << std::endl;
this->bm_.execute( *it );
}
}
// You'll notice the list is templated for a string,
// and not a Command class. So I'm passing a string
// directory into the BankMenu::execute.
// No errors were thrown from Visual Studio, it's
// obviously doing some magic behind the scene.
// It sees that I'm passing in a string, but expect
// a Command type, since I have a constructor for the
// Command class that handles a string type, Visual
// Studios (the compiler, not the IDE) seems to me
// change my code to something like the following:
/*
void
Simulator::run ()
{
// this->bm_ is a BankMenu object.
List<std::string>::iterator it = this->commands_.begin();
std::cout << *it << std::endl;
for(; it != this->commands_.end(); it++)
{
std::cout << " > " << *it << std::endl;
Command __tmp (*it);
this->bm_.execute( __tmp );
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment