Difference between revisions of "Talk:CPP/Classes/ConstructorInheritance"

From ProgrammingExamples
Jump to: navigation, search
(Created page with 'both ctors in the subclasses Pilot and BlimpPilot set the Person's member variable m_Name<br> <code> Pilot(std::string &name) : Person(name) {m_Name = name;} BlimpPilot(s…')
(No difference)

Revision as of 17:25, 3 July 2010

both ctors in the subclasses Pilot and BlimpPilot set the Person's member variable m_Name

   Pilot(std::string &name) : Person(name) {m_Name = name;}
   BlimpPilot(std::string &name) : Pilot(name) {m_Name = name;}

this is not needed and already done by the Person ctor :

   Person(std::string name)
   {
     m_Name = name;
   }

thus, to avoid unneeded (and error prone) work , the ctors should be

   Person(std::string name){m_Name = name;}        // does the affectation of m_Name
   Pilot(std::string &name) : Person(name) {}      // calls 'Person' ctor
   BlimpPilot(std::string &name) : Pilot(name) {}  // calls Pilot' ctor