/home/ztteng/work/test/20130814/inc/PartList.h: In constructor ‘CarPart::CarPart(int, int)’:
/home/ztteng/work/test/20130814/inc/PartList.h:21: warning: ‘CarPart::itsModelYear’ will be initialized after
/home/ztteng/work/test/20130814/src/PartList.cpp:10: warning: base ‘Part’
/home/ztteng/work/test/20130814/src/PartList.cpp:9: warning: when initialized here
#include <iostream> using namespace std; class Part { private: int itsPartNumber; public: Part():itsPartNumber(1){} Part(int PartNumber):itsPartNumber(PartNumber){} virtual ~Part(){} int GetPartNumber() const {return itsPartNumber;} virtual void Display() const =0; }; class CarPart : public Part { private: int itsModelYear; public: CarPart():itsModelYear(94){} CarPart(int year,int partNumber); virtual void Display() const { Part::Display(); cout<<"Model Year: "; cout<<itsModelYear<<endl; } };
构造函数实现的时候
CarPart::CarPart(int year, int partNumber):Part(partNumber),itsModelYear(year) {}
其实g++给的这个warning就是为了提醒你,初始化列表中数据成员的初始化顺序应该和你在类的声明中给出数据成员的顺序一致!
由于CarPart继承Part;所以当CarPart初始化构造函数的时候的;先初始化Part成员变量再初始化CarPart成员变量;否则则会像开头那样提示的warning