C ++初始化列表与分配值 [英] C++ initialization list vs assigning values

查看:66
本文介绍了C ++初始化列表与分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,初始化列表和在构造方法中分配值之间的区别是什么,而不是每种方法的外观如何?我的意思是使用一个而不是另一个的优点是什么?为什么在幻灯片中的给定示例中(以下)仅适用于初始化?(我希望您可以添加一些资源,因为我没有找到它)

in C++, what is the difference between initialization list and assigning values in a constructer rather than the way each method looks? I mean what's the advantage of using one rather than the other and why in the given example in the slide (below) only works with initialization? (I hope if you could add some resources to it since I didn't find)

单击此处查看幻灯片:在imgur上上传

推荐答案

在构造函数中使用初始化列表是一个一步的过程,即它在声明对象时对其进行初始化.它调用复制构造函数.

Using initialization list in constructor is the one step process i.e. it initializes objects at the moment it’s declared. It calls copy constructor.

使用赋值是两步过程,即定义对象然后赋值.定义对象将调用默认构造函数,然后赋值将调用赋值运算符.因此,操作成本很高.

Whereas using assignment is the two-step process i.e. define the object and then assign it. Defining objects calls default constructor and then assignment calls assignment operator. Hence, expensive operations.

在C ++中,只能在初始化列表中初始化类的常量或引用数据成员变量,而不能在构造函数主体中使用赋值.

In C++, constant or reference data member variables of a class can only be initialized in the initialization list, not using assignment in constructor body.

常量和引用数据成员变量均具有必须在声明时进行初始化的属性.因此,只有在构造函数中使用初始化列表的方法,因为初始化列表在声明时初始化类成员变量,而在构造函数主体在声明之后初始化数据成员时进行赋值.

Both constant and reference data member variables have property that they both must be initialized at the moment of declaration. So, there is only way to use initialization list in constructor, as initialization list initializes class member variables at the time of declaration whereas assignment if constructor body initializes data members after declaration.

在某些情况下,初始化构造函数中的数据成员无效,必须使用初始化列表.以下是这种情况.

There are situations where initialization of data members inside constructor doesn’t work and Initializer List must be used. Following are such cases.

  1. 用于初始化非静态const数据成员.

#include<iostream> 
using namespace std; 

class Test { 
    const int t; 
public: 
    Test(int t):t(t) {}  //Initializer list must be used 
    int getT() { return t; } 
}; 

int main() { 
    Test t1(10); 
    cout<<t1.getT(); 
    return 0; 
}

  1. 用于引用成员的初始化.

#include<iostream> 
using namespace std; 

class Test { 
    int &t; 
public: 
    Test(int &t):t(t) {}  //Initializer list must be used 
    int getT() { return t; } 
}; 

int main() { 
    int x = 20; 
    Test t1(x); 
    cout<<t1.getT()<<endl; 
    x = 30; 
    cout<<t1.getT()<<endl; 
    return 0; 
}

  1. 用于初始化没有默认构造函数的成员对象.(在您的情况下,数组数字没有默认的构造函数)
  1. For initialization of member objects which do not have default constructor. (In your case Array digits does not have default constructor)

#include <iostream>
using namespace std;
class A { 
    int i; 
public: 
    A(int ); 
}; 

A::A(int arg) { 
    i = arg; 
    cout << "A's Constructor called: Value of i: " << i << endl; 
} 

// Class B contains object of A 
class B { 
    A a; 
public: 
    B(int ); 
}; 

B::B(int x):a(x) {  //Initializer list must be used 
    cout << "B's Constructor called"; 
} 

int main() { 
    B obj(10); 
    return 0; 
}

  1. 用于初始化基类成员.
  2. 当构造函数的参数名称与数据成员相同时.
  3. 出于性能方面的考虑.

这篇关于C ++初始化列表与分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆