C ++初始化 [英] C++ initialization

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

问题描述


可能重复:

初始值的类构造

http://en.wikipedia.org/wiki/Delegation_pattern 中的c ++示例我注意到了我所避免的问题之前看过:

While I was looking at c++ example in http://en.wikipedia.org/wiki/Delegation_pattern I noticed something I haven't seen before:

C() : i(new A()) { }

我的问题是:这行代码与以下代码有什么不同:

My question is: How is this line of code any different from:

C() {
    i = new A();
}

后的构造函数? new A()的括号是什么?

What does : after constructor do? What does the brackets around new A() do?

推荐答案

名为初始化列表。它用于初始化类的数据成员。

Its called initialization list. It is used to initialize the data members of a class.

C() {
    i = new A(); // i is not initialized here, here assignment is taking place.
}

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

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