18-初始化列表

寫在前面

初始化列表,旨在初始化的時候為屬性賦值

碼上建功

//先看一個最基本的構造函數(shù),帶初始化屬性列表的
struct Person {
int m_age;
int m_height;

Person() {
    this->m_age = 10;     //初始化賦值,只能用this獲得屬性,this類似于OC中的self
    this->m_height = 20;
}
void display() {
    cout << "m_age is " << this->m_age << endl;
    cout << "m_height is " << this->m_height << endl;
}

};
運行一下
Person person;
person.display();
看下打印結果:
m_age is 10
m_height is 20

//初始化的時候給屬性賦值
struct Person {
int m_age;
int m_height;
// 初始化列表 :m_age(age), m_height(height)
//用一個冒號隔開,前面是需要傳入的參數(shù),后面是要賦值的屬性
Person(int age, int height) :m_height(height), m_age(age) {
//對屬性進行一些加工操作
}
void display() {
cout << "m_age is " << this->m_age << endl;
cout << "m_height is " << this->m_height << endl;
}
};
使用
Person person2(15, 25);
person2.display();
打印結果
m_age is 15
m_height is 25

當然在初始化的時候也可以通過函數(shù)調(diào)用返回初始化列表的值

int myAge() {
cout << "myAge()" << endl;
return 30;
}

int myHeight() {
cout << "myHeight()" << endl;
return 180;
}
struct Person {
int m_age;
int m_height;
// 初始化列表 :m_age(age), m_height(height)
//用一個冒號隔開,前面是需要傳入的參數(shù),后面是要賦值的屬性
Person():m_height(myHeight()), m_age(myAge()) {

}
void display() {
    cout << "m_age is " << this->m_age << endl;
    cout << "m_height is " << this->m_height << endl;
}

};
調(diào)用
Person person;
person.display();
打印結果:
myAge()
myHeight()
m_age is 30
m_height is 180

當然你也可以這樣來初始化
struct Person {
int m_age;
int m_height;

Person(int age, int height) {
 cout << "Person(int age, int height) " << this << endl;
 this->m_age = age;
 this->m_height = height;
 }

void display() {
    cout << "m_age is " << this->m_age << endl;
    cout << "m_height is " << this->m_height << endl;
}

};
調(diào)用
Person person2(15, 25);
person2.display();
打印
m_age is 15
m_height is 25

## 多個初始化列表方法時

struct Person {
int m_age;
int m_height;

Person() :Person(0, 0) { }
Person(int age, int height) :m_age(age), m_height(height) { }

void display() {
    cout << "m_age is " << this->m_age << endl;
    cout << "m_height is " << this->m_height << endl;
}

};
調(diào)用:
Person person;
person.display();
Person person2(15, 25);
person2.display();
打印結果:
m_age is 0
m_height is 0
m_age is 15
m_height is 25

## 如果函數(shù)聲明和實現(xiàn)是分離的

struct Person {
int m_age;
int m_height;
// 默認參數(shù)只能寫在函數(shù)的聲明中
Person(int age = 0, int height = 0);
};

// 構造函數(shù)的初始化列表只能寫在實現(xiàn)中
Person::Person(int age, int height) :m_age(age), m_height(height) {

}
調(diào)用
Person person;
person.display();
Person person2(10);
person2.display();
Person person3(20, 180);
person3.display();
打印結果:
m_age is 0
m_height is 0
m_age is 10
m_height is 0
m_age is 20
m_height is 180

裝逼一下

一種便捷的初始化成員變量的方式
只能用在構造函數(shù)中
初始化順序只跟成員變量的聲明順序有關
? 如果函數(shù)聲明和實現(xiàn)是分離的
初始化列表只能寫在函數(shù)的實現(xiàn)中
默認參數(shù)只能寫在函數(shù)的聲明中

完整代碼demo,請移步GitHub:DDGLearningCpp

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容