Display the enum value that is passed to c++ class constructor -


i have written short test code passing enum value class constructor. works compiler. however, output weird. display() not show enum value. shows "the strategy of current agent ". what's wrong code? thank you!

#include <iostream>  using namespace std;  class agent { public:     enum strat {buyandhold, momentum, ta};     agent(strat strategy=buyandhold);     ~agent();     void display(); private:     strat strategy; };  agent::agent(strat strategy) {     strategy = strategy; }  agent::~agent() {     cout << "bye!" << endl; }  void agent::display() {     cout << "the strategy of current agent ";     switch(strategy){     case buyandhold : cout << "buyandhold." << endl; break;     case momentum : cout << "momentum." << endl; break;     case ta : cout << "ta." << endl; break;     } }  int main() {     agent a(agent::ta);     a.display();     return 0; } 

you need distinguish local parameter name declarations class members. provide different name initialization parameter:

agent::agent(strat strategy_) : strategy(strategy_) {} 

i prefer _ postfix either mark class member variables, or parameter names, since either way standard compliant (using such _ prefix won't be, , else e.g. m_ prefix class member variables clumsy imho).

also note:
i've been using member initializer list assign class member variable, that's right choice of use cases.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -