C++构造函数初始化列表与赋值

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

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

父类——子类——……

所以必须:

class Dog: public Animal
{
public:
    Dog(int weight,int height,int type):
        Animal(weight,height)         //必须使用初始化列表增加对父类的初始化
    {
        ;
    }
private:
    int m_type;
};
在百度知道中搜到一个解释:http://zhidao.baidu.com/question/199817874.html?qbl=relate_question_3
构造一个对象是从内向外构造的,也就是先构造基类,再构造派生类。因此必须在派生类构造函数执行前先执行基类构造函数。

你的那个例子因为构造函数都是最简单的无参数类型,所以无所谓,不需要额外明白的写出来,C++会默认帮你调用无参数的base()。实际上完整的写法应该是这样的:
class follow
{
        public:
                  follow() : base() // 注意这里 .
                  {
                  }
}
只不过你不这么写C++也会默认帮你这么调用而已。
如果基类的构造函数是带参数的,那就必须明白写出来了:
class base
{
      public:
                base(int i){}
};
class follow
{
        public:
                  follow(int i) : base(i) // 这里必须明确指定如何调用基类的构造函数 .
                 {
                 }
}

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_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)拷贝构造函数完成初始化

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值