#include using namespace std; //class class fruit { private: string name; int money; public: //先測試this是什麼 void showthis() { cout<<"this = "<name = name; //(3)*this = 這個物件變數 (*this).name = name; //money this->money = money; //結論:方法2最常用(this->name) } //建構子函數(),沒有參數 fruit() { this->name = ""; this->money = 0; } //傳遞回該物件 fruit showmoney() { cout<<"money = "<money<<"\t"; return (*this); } //傳遞回該物件 fruit showname() { cout<<"name = "<name<<"\t"; return (*this); } }; int main(int argc, char** argv) { //1.顯示this的本質 fruit apple; apple.showthis(); fruit grape; grape.showthis(); //this = 0x6ffe00 //this = 0x6ffdf0 //結論:物件裡面的this = 指標 //2.建構子函數,使用三種this指令指定參數:this->name, (*this).name cout<name = name來設定傳遞的參數值 //3.什麼時候才能連續執行三個物件函數 //例如:apple3.showname().showmoney().showthis() //方法:必須該函數傳遞回return該物件的函數,才能連續.....下去 //原理:只有傳遞回該物件,才能以物件.函數的方式,呼叫其它函數 cout< return (*this) //方法2:在函數最後傳回物件指標 ---> return (this) //4.物件陣列,10個蘋果 cout<showname()->showmoney()->showthis(); } return 0; }