C++文法学習ノート10:Inline、const、mutable、this、static

11349 ワード

インスタンスコード:

#include 
#include 
#include 

using namespace std;

class Time {

private:
	void initMillTime(int mls) {
		Millisecond = mls;
	}

	int Millisecond; //  
public:
	int Hour; //  
	int Minute;//  
	int Second;//  

	mutable int testVal;

	 //    
	Time(int temphour, int tmpmin, int tmpsec) {
		Hour = temphour;
		Minute = tmpmin;
		Second = tmpsec;
		initMillTime(0);

		cout << "       Time(int temphour, int tmpmin, int tmpsec)" << endl;
	}

	Time() {
		Hour = 12;
		Minute = 59;
		Second = 59;
		initMillTime(50);

		cout << "       Time()" << endl;
	}

	void addHour(int tmphour) {
		Hour += tmphour;
	}

	void addHour1(int tmphour) const{
		//Hour += tmphour; //                 
	}

	void fixVal() const {
		testVal = 10;
	}

	//           
	Time& add_hour(int tmphour) {
		Hour += tmphour;
		return *this;	//          
	}


	static int s_val;
};

int Time::s_val = 10;

int main() {

	// :           inline:                       
	//                 ,    inline       
	//    : addHour()  

	// :        const
	//                  const
	//  :    ,                         。
	//    : addHour1()  

	const Time abc;//  const  ,       
	//abc.addHour(15);  //   addHour()    const    ,  addHour    const    
	Time def;
	def.addHour(16); //    

	//const          const  ,   const,     const    。
	//  const    ,    const    ,    const    。


	// :mutable (   ,       ),const    ,mutable           const   
	// mutable        ,          mutable   ,   ,                 ,
	//		    const        ,     。
	//    :  fixVal()  
	const Time ghi;
	ghi.fixVal();


	// :          , this
	//      this,          ,             (&myTime)                this  。

	Time myTime;
	myTime.add_hour(3); //            add_hour(&myTime, 3);

	//(1) this            ,    ,         this  
	//(2)         ,this      const   const  (   Time,  this  Time* const this),  this      Time  
	//(3)  const     , this       const   const  (   Time,this  const Time* const this     )

	myTime.add_hour(3).add_hour(5);

	// :static   
	//           ,        static    
	//  :        ,     ,                     ,                 
	Time mytime1;
	mytime1.s_val = 100;

	Time mytime2;
	mytime2.s_val = 200;

	cout << mytime1.s_val << endl;  //   200
	cout << mytime2.s_val << endl;  //   200

	system("pause");
	return 0;
}