I made a mix today, I have been dizzy for a long time, the following is my question summary:
Class a {public: a (): m_run (false) {} Bool get Nunning () {Return M_Run;} protected: BOOL M_RUN;
Class B: Public a {public: b (): m_run (false) {} // Override from baseclass virtual void work (); protected: BOOL m_RUN;
Void class b :: Work () {m_run = true;
Void Runit (a * a) {a-> work (); if (getRunning () == false) cout << "WRONG"; else cout << "right" ;?}
INT main () {b * b = new b (); runit (b);}
Output: WRONG
I originally wanted Class B to control its base class Class A protected data member M_Run, and the result was unclear to derived class Class B added an identical data member M_Run. Although I have modified M_Run's value in the operation of the derived class, this data member is a derived class, and the derived class is overloaded by the base class, which is directly used by the derived class. member. However, derived classes can access the values of the base class data via getRunning, which is considered to be different. Therefore, when multi-state access by the base class pointer, the result of the function getRunning returned by the base class is the basic class data member M_Run, and the derived class does not affect the M_Run of the base class without the modification of the data member M_Run. Basic class data member M_Run After the default initialization is initialized by the constructor, the value has not changed, so the result of getRunning returns has always been false.
Lessons: 1) Try to place the data in the Private area of the class, so that the safer. 2) Never overload data members of the base class, this example is a lesson. 3) The function of this overload can be used to use the scope operation to distinguish them, such as A :: m_Run B :: m_run.