c ++空构造函数和成员初始化 [英] c++ empty constructor and member initialization
本文介绍了c ++空构造函数和成员初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有关于默认和空构造函数的混淆。空构造函数是否也自动初始化类变量?这意味着如果我使用一个空的构造函数而不是默认构造函数,那也自动初始化类成员变量?例如,如果使用下面的代码,整数指针是否初始化为NULL?请确认
// .h文件
Class Test {
public:
Test();
〜Test();
int * p;
}
// .cpp文件
Test :: Test()
{
//做某事..
}
否,空构造函数与默认构造函数相同(如果不初始化)其中的任何成员变量。
I have confusion regarding default and empty constructor. Does empty constructor also initializes class variable automatically ? Meaning if i use a empty constructor instead of default constructor , does that also initialize class member variable automatically ? For example, if use following code, does integer pointer is initialized to NULL ? Please confirm
// .h file
Class Test {
public:
Test();
~Test();
int *p;
}
// .cpp file
Test::Test()
{
// do something..
}
解决方案
No, empty constructor is same as default constructor if you don't initialize any member variable inside it.
这篇关于c ++空构造函数和成员初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文