C++构造函数初始化列表与构造函数中的赋值的区别

C++类中成员变量的初始化有两种方式:构造函数初始化列表和构造函数体内赋值。

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

class Animal
{
public:
    Animal(int weight, int height): // A初始化列表
        m_iWeight(weight),
        m_iheight(height)
    {
    }

    Animal(int weight, int height) // B函数体内初始化
    {
        m_iWeight = weight;
        m_iheight = height;
    }
private:
    int m_iWeight;
    int m_iheight;
};
// 对于这些内部类型来说,基本上是没有区别的,效率上也不存在多大差异,当然A和B方式不能共存的。

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

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无合适构造函数
    // {
    // }
// 上面的子类和父类编译会出错:
//   因为子类Dog初始化之前要进行父类Animal的初始化,但是根据Dog的构造函数,没有给父类传递参数,使用了父类Animal的无参数构造函数。而父类Animal提供了有参数的构造函数,
//   这样编译器就不会给父类Animal提供一个默认的无参数的构造函数了,所以编译时报错,说找不到合适的默认构造函数可用。要么提供一个无参数的构造函数,
//   要么在子类的Dog的初始化列表中给父类Animal传递初始化参数,如下:
    Dog(int weight, int height, int type): 
        Animal(weight, height)
    {
        this->m_type = type;
    }
private:
    int m_type;
};

三、类中const数据成员、引用数据成员,必须在初始化列表中初始化,不能使用赋值的方式初始化

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

// 构造函数不能被声明为const函数。
// 构造函数需要初始化成员变量,如果声明为const函数,则无法修改成员变量;
// 由于常量只能初始化不能赋值,所以常量成员必须使用初始化列表

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

#include <iostream>

class Food
{
public:
    Food(int type = 10)
    {
        std::cout << "Food construct..." << std::endl;
        m_type = type;
    }
    Food(Food & other) //拷贝构造函数
    {
        std::cout << "Food copy construct..." << std::endl;
        m_type = other.m_type;
    }
    Food & operator = (Food & other) //重载赋值=函数
    {
        std::cout << "Food operator =" << std::endl;
        m_type = other.m_type;
        return *this;
    }
private:
    int m_type;
};

// (1)、构造函数赋值方式初始化成员对象m_food

class Rice: public Food
{
public:
    Rice(Food &food) //: m_food(food)//初始化 成员对象
    {
        std::cout << "Rice construct..." << std::endl;
        m_food = food; //初始化 成员对象
    }
private:
    Food m_food;
};

// (2)、构造函数初始化列表方式

// class Rice: public Food
// {
// public:
//     Rice(Food &food) : m_food(food)//初始化 成员对象
//     {
//         std::cout << "Rice construct..." << std::endl;
//         // m_food = food; //初始化 成员对象
//     }
// private:
//     Food m_food;
// };

int main()
{
    Food fd;
    Rice rice(fd);
    return 0;
}

// 第一种方法结果:
// Food construct...
// Food construct...
// Food construct...
// Rice construct...
// Food operator =

// 第二种方法结果:
// Food construct...
// Food construct...
// Food copy construct...
// Rice construct...

// /不同的初始化方式得到不同的结果:明显构造函数初始化列表的方式得到更高的效率。

原文链接: https://www.cnblogs.com/vivian187/p/12736988.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++构造函数初始化列表与构造函数中的赋值的区别

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/343248

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午2:00
下一篇 2023年3月2日 上午2:01

相关推荐