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

  • 构造函数初始化列表;
  • 构造函数体内赋值; 

 


 内部数据类型

形如char,int,float…指针等

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方式不能共存的。


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

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

 这种必须在派生类中构造函数中初始化提供父类的初始化,因为对象构造的顺序是:

父类——子类——……

 

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

类中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;//在c++11中可以这样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_food
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(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)拷贝构造函数完成初始化

成员初始化列表和构造函数体的区别

成员初始化列表和构造函数的函数体都可以为我们的类数据成员指定一些初值,但是两者在给成员指定初值的方式上是不同的。成员初始化列表使用初始化的方式来为数据成员指定初值,而构造函数的函数体是通过赋值的方式来给数据成员指定初值。也就是说,成员初始化列表是在数据成员定义的同时赋初值,但是构造函的函数体是采用先定义后赋值的方式来做。这样的区别就造成了,在有些场景下,是必须要使用成员初始化列表。

 

成员初始化列表的行为

成员初始化列表是可以初始化类的数据成员,那么他是如何操作的呢?是通过一系列的函数调用么,不是的。成员初始化列表是按照数据成员的声明顺序,将初始化操作安排在构造函数所有usercode的前面。成员初始化列表的初始化顺序是按照类成员的声明顺序来的,所以在初始化的时候,尽量不要用次序较后的成员来初始化次序较前的成员,这样就会出问题,这也是成员初始化列表的一个弊端。

 ******************************************更新*******************************************************

在类中定义vector、数组等并初始大小

vector 是STL中的容器类,包含多种通用算法,可以通过在构造函数进行初始化。

class base{
public:
    base():a(10){}
private:
    int n;
    vector<int> a;
};

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐