foobar

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  6 随笔 :: 14 文章 :: 0 评论 :: 0 Trackbacks

Initializer lists

In C++, it is the order of the class inheritance and of the member variables that determine the initialization order, not the order of an initializer list:



#include 
<iostream>

class CSomeClass
{
public:
CSomeClass(
int n)
{
std::cout 
<< "CSomeClass constructor with value ";
std::cout 
<< n << std::endl;
}

}
;

class CSomeOtherClass
{
public:
CSomeOtherClass() 
//In this example, despite the list order,
: obj2(2), obj1(1//obj1 will be initialized before obj2.
{
//Do nothing.
}

private:
CSomeClass obj1;
CSomeClass obj2;
}
;

int main(void)
{
CSomeOtherClass obj;
return 0;
}

posted on 2007-06-04 11:11 foobar 阅读(182) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理