[英]where are the non-static member variables of a class initialized?
初始化类的非静态成员变量在哪里?
在类声明内或构造函数内? 谢谢
在构造函数中。 构造函数用于初始化类的非静态成员。
class foo
{
static int num; // static variable don't belong to any particular instance of a class.
foo(){}
};
所以,在相应的源文件中这样做 -
int foo::num = 10 ;
它在构造函数的初始化列表中。 如果使用编译器生成的构造函数,那么原理是相同的,它只是隐式生成的。
最好在构造函数初始化列表中: http : //www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.