一:this指針
class Stutent
{
private:
int number;
string name;
public:
string name() { return name; }
};
在上面的代碼中,假設定義一個對象Stutent limei; 然後執行limei.name(),則會返回對象limei的成員變量name。成員函數name()通過this指針判斷是哪個對象的name。this是一個隱式參數,是一個指向調用函數的對象的地址的指針,this的類型是Stutent * const this (注意頂層const指針的聲明中解引用符號的位置)。當執行limei.name()的時候,等價於執行Student::name(&limei)。而函數部分return name 等價於return this->name。
二:類作用域和成員函數
編譯器分兩步編譯類,首先編譯成員的聲明,然後纔是編譯成員函數體。
三:構造函數初始值列表
設有一個類,類名叫做Person,則下面這行代碼定義了一個默認構造函數,其中=default是c++11的新標準。
Person() = default;