C++-什么时候需要在类的构造函数中使用初始化列表
1,如果基类没有default构造函数,则意味着其不能自己初始化。如果其被派生,派生类的构造函数要负责调用基类的构造函数,并传递给它需要的参数。下例中Base
2,如果类成员没有默认构造函数。下例中Elem4
2,如果类的成员变量中含有const成员变量,如果不使用列表,在构造函数中是不能对其赋值的,会导致编译失败。下例中b
3,如果类的成员变量中含有引用,引用必须被初始化。下例中c
4,需要提高效率的时候,如果不使用初始化列表,而放在构造函数体内赋值的方法,则变量先被默认构造函数初始化,然后又调用copy构造函数。
///////////////////////////////////////////////////////////////////////////////
//
// FileName : constructor.h
// Version : 0.10 created 2013/11/09 00:00:00
// Author : Jimmy Han
// Comment :
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std; class Elem1{
public:
Elem1(int x){
cout << "Elem1() was called." << endl;
}
~Elem1(){
cout << "~Elem1() was called." << endl;
}
}; class Elem2{
public:
Elem2(int x){
cout << "Elem2() was called." << endl;
}
~Elem2(){
cout << "~Elem2() was called." << endl;
}
}; class Elem3{
public:
Elem3(int x){
cout << "Elem3() was called." << endl;
}
~Elem3(){
cout << "~Elem3() was called." << endl;
}
}; class Elem4{
public:
Elem4(int x){
cout << "Elem4() was called." << endl;
}
~Elem4(){
cout << "~Elem4() was called." << endl;
}
}; class Base{
public:
Base(int):_elem2(Elem2()), _elem1(Elem1()), _elem3(Elem3()){
cout << "Base() was called." << endl;
}
~Base(){
cout << "~Base() was called." << endl;
}
private:
Elem1 _elem1;
Elem2 _elem2;
Elem3 _elem3;
}; class Derive : public Base{
public:
//if there is no default constructor for base class, it has to be called explicit in derive class
//four scenarios to use initialization list must:
//1. Base. base class don't have Base();
//2. const int b. class member is const
//3. int& c. class member is reference
//4. _elem4. class member dont' have default constructor.
Derive():Base(), _elem4(Elem4()), b(), c(b){
cout << "Derive() was called." << endl;
}
private:
Elem4 _elem4;
const int b;
const int& c; };
///////////////////////////////////////////////////////////////////////////////
//
// FileName : constructor_client.cc
// Version : 0.10
// Author : Ryan Han
// Date : 2013/11/19
// Comment :
// Elem1() was called.
// Elem2() was called.
// Elem3() was called.
// Base() was called.
// Elem4() was called.
// Derive() was called.
// ~Elem4() was called.
// ~Base() was called.
// ~Elem3() was called.
// ~Elem2() was called.
// ~Elem1() was called.
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "constructor.h"
using namespace std; int main(){
Derive d1; return ;
}
C++-什么时候需要在类的构造函数中使用初始化列表的更多相关文章
- C++ 构造函数的对象初始化列表
//构造函数的对象初始化列表 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class P ...
- C++定义构造函数必须使用初始化列表的场合
明其理,而知其然也. 先给理论.1. 初始化 != 赋值. a.初始化代表为变量分配内存. 变量在其定义处被编译器初始化(编译时). 在函数中, 函数参数初始化发生在函数调用时(运行时). b.赋值代 ...
- Java类继承关系中的初始化顺序
Java类初始化的顺序经常让人犯迷糊,现在本文尝试着从JVM的角度,对Java非继承和继承关系中类的初始化顺序进行试验,尝试给出JVM角度的解释. 非继承关系中的初始化顺序 对于非继承关系,主类Ini ...
- 【C++】类的特殊成员变量+初始化列表
参考资料: 1.黄邦勇帅 2.http://blog.163.com/sunshine_linting/blog/static/448933232011810101848652/ 3.http://w ...
- C++中使用初始化列表比在构造函数中对成员变量赋值更高效
这是在面试中遇到的一个问题,没有答出来,后来上网上查了一些资料,终于弄明白了: 一.首先c++标准规定成员变量必须在调用构造函数前进行初始化(这一点很重要) 二.如果我们在构造函数中对成员变量进行初始 ...
- 【深入理解JVM】:Java类继承关系中的初始化顺序
尝试着仔细阅读thinking in java 看到一篇很好的文章http://blog.csdn.net/u011080472/article/details/51330114
- 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...
- C++学习17派生类的构造函数
基类的构造函数不能被继承,在声明派生类时,对继承过来的成员变量的初始化工作也要由派生类的构造函数来完成.所以在设计派生类的构造函数时,不仅要考虑派生类新增的成员变量,还要考虑基类的成员变量,要让它们都 ...
- C++类构造函数初始化列表
C++类构造函数初始化列表 构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式.例如: class CExample {public: ...
随机推荐
- 2年后的Delphi XE6
1.有幸下载到Delphi XE6,下载地址如下: http://altd.embarcadero.com/download/radstudio/xe6/delphicbuilder_xe6_win. ...
- openid4java 使用记录[转载]
[文章来源:http://r-j-r-a5438-163-com.iteye.com/blog/611351] 在项目中使用了openid4java进行开发,在开发过程中碰到过一些问题,在网上也找了很 ...
- 如何连接git bash和git hub
git config --global user.name "Your Real Name" 2 git config --global user.email you@email. ...
- window2012 64bit 安装sqlserver2012 64bit调用excel的驱动安装
如果电脑已经安装了32bit的office那么可以使用 管理员权限打开cmd,然会执行以下命令 AccessDatabaseEngine_X64.exe /passive
- The Economist
The turning point in the process of growing up is when you discover the core of strength within yo ...
- python实现微信打飞机游戏
环境:Ubuntu 16.04 LTS Python 2.7.11 + Pygame + Pycharm 代码: # -*- coding: UTF-8 -*- import pygame, ran ...
- --专访雷果国: 从1.5K到18K 一个程序员的5年成长之路--
导语:今年三月份,在CSDN博客和新浪微博上有一篇<从1.5K到18K,一个程序员的5年成长之路>被众人分享和传阅,这篇博文首先介绍了作者自学之初薄弱的基础,然后通过流水账形式分享了那个从 ...
- java多线程的常用方法(以及注意事项)
/* * 线程的常用方法 * 1.start(); * 2.run(); * 3.sleep(int millsecond); * 4.isAlive(); -->判断线程是否还在运行 * 5. ...
- 关于gridview 实现查询功能的方法
protected void btnSearch_Click(object sender, EventArgs e) { TestCon(); } protected void btnAllData_ ...
- 问题解决The connection to adb is down, and a severe error has occured.
遇到问题描述: 运行android程序控制台输出 [2013-06-25 11:10:32 - MyWellnessTracker] The connection to adb is down, an ...