帮忙看一下这里常成员变量怎么可以更新了
为什么常成员变量也可以更新了啊 *a.h class TDate1 { public: TDate1 (int y=2005, int m=10); TDate1 (int y, int m, int d); ~TDate1 (); void print (); void print()const; static void GetNum(); friend void SetNum(int n); private: const int a; int year,month,day; static int num; }; *a.cpp #include <iostream.h> #include "a.h" int TDate1::num=2; TDate1::TDate1(int y, int m):a(y) { year =y; month =m; cout << "Constructor1 called "<<endl; } TDate1::TDate1(int y, int m, int d):a(y) { year =y; month =m; day =d; cout << "Constructor2 called "<<endl; } TDate1::~TDate1() { cout << "Destructor called "<<endl; } void TDate1::print() { cout <<year<<"."<< month <<"."<<a<<endl; } void TDate1::print()const { cout<<year<<"."<<month<<"."<<day<<"."<<a <<endl; } void TDate1:: GetNum() { cout<<num<<endl;} void SetNum(int n) { TDate1::num=n;} *main.cpp #include <iostream.h> #include "a.h" void main () { TDate1 today; today.print(); TDate1 tomorrow(2008); tomorrow.print(); TDate1::GetNum(); SetNum(4); TDate1::GetNum(); SetNum(8); TDate1::GetNum(); TDate1 const Next(2005,11,11); Next.print(); } 结果 Constructor1 called 2005.10.2005 Constructor1 called 2008.10.2008 2 4 8 Constructor2 called 2005.11.11.2005 Destructor called Destructor called Destructor called Press any key to continue |