美文网首页
18-初始化列表

18-初始化列表

作者: _东阁堂主_ | 来源:发表于2019-02-21 13:08 被阅读0次

写在前面

初始化列表,旨在初始化的时候为属性赋值

码上建功

//先看一个最基本的构造函数,带初始化属性列表的
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)
//用一个冒号隔开,前面是需要传入的参数,后面是要赋值的属性
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

当然在初始化的时候也可以通过函数调用返回初始化列表的值

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)
//用一个冒号隔开,前面是需要传入的参数,后面是要赋值的属性
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;
}

};
调用
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;
}

};
调用
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;
}

};
调用:
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

如果函数声明和实现是分离的

struct Person {
int m_age;
int m_height;
// 默认参数只能写在函数的声明中
Person(int age = 0, int height = 0);
};

// 构造函数的初始化列表只能写在实现中
Person::Person(int age, int height) :m_age(age), m_height(height) {

}
调用
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


## 装逼一下

一种便捷的初始化成员变量的方式 
只能用在构造函数中 
初始化顺序只跟成员变量的声明顺序有关
◼ 如果函数声明和实现是分离的 
初始化列表只能写在函数的实现中 
默认参数只能写在函数的声明中
 

完整代码demo,请移步GitHub:[DDGLearningCpp](https://github.com/dudongge/DDGLearningCpp)

相关文章

  • 18-初始化列表

    写在前面 初始化列表,旨在初始化的时候为属性赋值 码上建功 //先看一个最基本的构造函数,带初始化属性列表的str...

  • 18-初始化列表

    写在前面 码上建功 //先看一个最基本的构造函数,带初始化属性列表的struct Person {int m_ag...

  • [C++之旅] 11 初始化列表

    [C++之旅] 11 初始化列表 初始化列表的特性 初始化列表先于构造函数执行 初始化列表只能用于构造函数 初始化...

  • C++的初始化列表和列表初始化

    C++的初始化列表和列表初始化 初始化列表 初始化列表是声明在构造函数中来实现的,相当于初始化,而不是复制操作 初...

  • C++初始化列表

    引言 用c++的人都知道,c++的构造函数具有初始化列表,初始化列表有什么作用?什么情况下必须使用初始化列表...

  • C++11——变量和基础类型

    列表初始化(List Initialization) 当初始化列表与内置类型的变量一起使用时,这种初始化形式具有一...

  • c++11新特性之列表初始化

    C++11新增了列表初始化的概念。 在C++11中可以直接在变量名后面加上初始化列表来进行对象的初始化。 列表初始...

  • C++11中的初始化列表

    1 初始化列表 C++98/03中可以使用初始化列表对普通数组、POD类型等进行初始化。如: 但是这种初始化方式的...

  • 2020-09-15 构造函数初始化列表

    使用初始化列表的构造函数,其显式地对成员进行初始化 未使用初始化列表的构造函数,其通过赋值来对成员进行初始化(此赋...

  • 类的组合

    组合类构造函数一般的定义形式如下: 内嵌对象的初始化使用了 初始化列表 来完成——内嵌对象必须使用初始化列表来实现...

网友评论

      本文标题:18-初始化列表

      本文链接:https://www.haomeiwen.com/subject/gvsryqtx.html