开发者

where are the non-static member variables of a class initialized?

where are the non-static member variables of a class initialized?

within the class declaration or 开发者_StackOverflow社区within the constructor?? thanks


In the Constructor. Constructor is there to initialize the non-static members of class.


class foo
{
    static int num; // static variable don't belong to any particular instance of a class.
    foo(){}
};

So, do it like this in the corresponding source file -

int foo::num = 10 ;


It's within the constructor's initialization list. If a compiler-generated constructor is used then the principle is the same, it's just implicitly generated.


Preferrably in a constructor initialization list: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜