博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++构造函数初始化列表与构造函数中的赋值的区别
阅读量:2393 次
发布时间:2019-05-10

本文共 2361 字,大约阅读时间需要 7 分钟。

C++类中成员变量的初始化有两种方式:

         构造函数初始化列表和构造函数体内赋值下面看看两种方式有何不同。

         成员变量初始化的顺序是按照在那种定义的顺序。

1、内部数据类型(char,int……指针等)

class Animal{public:    Animal(int weight,int height):       //A初始化列表      m_weight(weight),      m_height(height)    {    }    Animal(int weight,int height)       //B函数体内初始化    {        m_weight = weight;        m_height = height;    }private:    int m_weight;    int m_height;};

对于这些内部类型来说,基本上是没有区别的,效率上也不存在多大差异。

当然A和B方式不能共存的。

 

2、无默认构造函数的继承关系中

class Animal{public:    Animal(int weight,int height):        //没有提供无参的构造函数       m_weight(weight),      m_height(height)    {}private:    int m_weight;    int m_height;};class Dog: public Animal{public:    Dog(int weight,int height,int type)   //error 构造函数 父类Animal无合适构造函数    {    }private:    int m_type;};

上面的子类和父类编译会出错:

因为子类Dog初始化之前要进行父类Animal的初始化,但是根据Dog的构造函数,没有给父类传递参数,使用了父类Animal的无参数构造函数。而父类Animal提供了有参数的构造函数,这样编译器就不会给父类Animal提供一个默认的无参数的构造函数了,所以编译时报错,说找不到合适的默认构造函数可用。要么提供一个无参数的构造函数,要么在子类的Dog的初始化列表中给父类Animal传递初始化参数,如下:

class Dog: public Animal{public:    Dog(int weight,int height,int type):        Animal(weight,height)         //必须使用初始化列表增加对父类的初始化    {        ;    }private:    int m_type;};
 

3、类中const常量,必须在初始化列表中初始,不能使用赋值的方式初始化

class Dog: public Animal{public:    Dog(int weight,int height,int type):        Animal(weight,height),         LEGS(4)                //必须在初始化列表中初始化    {        //LEGS = 4;           //error    }private:    int m_type;    const int LEGS;};

4、包含有自定义数据类型(类)对象的成员初始化        

class Food{public:    Food(int type = 10)    {        m_type = 10;    }    Food(Food &other)                 //拷贝构造函数    {        m_type = other.m_type;    }    Food & operator =(Food &other)      //重载赋值=函数    {        m_type = other.m_type;        return *this;    }private:    int m_type;};(1)构造函数赋值方式 初始化成员对象m_foodclass Dog: public Animal{public:    Dog(Food &food)      //:m_food(food)      {        m_food = food;               //初始化 成员对象    }private:    Food m_food;};//使用Food fd;Dog dog(fd);   //Dog dog(fd);结果:先执行了   对象类型构造函数Food(int type = 10)——> 然后在执行 对象类型构造函数Food & operator =(Food &other)想象是为什么?(2)构造函数初始化列表方式class Dog: public Animal{public:    Dog(Food &food)      :m_food(food)                  //初始化 成员对象    {        //m_food = food;                   }private:    Food m_food;};//使用Food fd;Dog dog(fd);   //Dog dog(fd);结果:执行Food(Food &other)拷贝构造函数完成初始化

不同的初始化方式得到不同的结果:

      明显构造函数初始化列表的方式得到更高的效率。

你可能感兴趣的文章
进程管理工具--supervisor
查看>>
使用virtualenv在ubuntu上搭建python-3开发环境
查看>>
详解-Python-的-“==”-和-“is”
查看>>
Tensorflow-Python-API-翻译(array_ops)
查看>>
Tensorflow-Python-API-翻译(constant_op)
查看>>
Tensorflow-Python-API-翻译(framework)
查看>>
Tensorflow-Python-API-翻译(math_ops)(第二部分)
查看>>
Tensorflow-Python-API-翻译(math_ops)(第一部分)
查看>>
Tensorflow-Python-API-翻译(nn)
查看>>
Tensorflow-Python-API-翻译(sparse_ops)
查看>>
论文阅读---An-Artificial-Neural-Network-based-Stock-Trading-System-Using-T
查看>>
A-Paper-A-Day--#1-Convolutional-Sequence-to-Sequence-Learning
查看>>
7个很棒的-chatbot-应用场景
查看>>
标记问题:词性标注(POS)和命名实体识别(NER)
查看>>
标记问题:介绍
查看>>
标记问题:生成模型和噪声通道模型
查看>>
机器学习算法在文本分类中的应用综述
查看>>
利用-TensorFlow-构建卷积神经网络
查看>>
利用-TensorFlow-实现排序和搜索算法
查看>>
利用TensorFlow实现卷积神经网络做文本分类
查看>>