vendredi 28 août 2015

declaring a pointer or a stack variable

Assume we have the following case:

  • If it is morning mood is good otherwise mood is bad
  • Print the mood

it could be written as:

std::string mode; //this cost us calling the constructor of std::string
if(time==Morning){ // a real code should be here
   mood="Good"//re assign the string
}
else{
   mood="bad"//re assign the string
}
std::cout << mood;

assuming it is not std::string, it is a very big object. Isn't it too luxury to declare "mode" and call the constructor for no reason! Is using the raw pointer a solution? what is the best practice here? thanks

Aucun commentaire:

Enregistrer un commentaire