构造函数的初始化程序

时间:2013-11-06 13:41:49

标签: c++ methods constructor initializer

首先,对于想要对此进行投票的人,请注意,成员变量未显示在类引用中,并且指向头文件的链接已被破坏!

我有以下构造函数:

    Foam::IOobject::IOobject
143 (
144  const word& name,           
145  const fileName& instance,   
146  const objectRegistry& registry,   
147  readOption ro,
148  writeOption wo,
149  bool registerObject
150 )
151 :                               
152  name_(name),                         
153  headerClassName_(typeName),
154  note_(),
155  instance_(instance),
156  local_(),
157  db_(registry),
158  rOpt_(ro),
159  wOpt_(wo),
160  registerObject_(registerObject),
161  objState_(GOOD)
162 {
163  if (objectRegistry::debug)
164  {
165  Info<< "Constructing IOobject called " << name_
166  << " of type " << headerClassName_
167  << endl;
168  }
169 }

据我所知,初始化程序用于:

  • 从派生类
  • 调用基类构造函数
  • 初始化类
  • 的成员变量

请参阅:https://stackoverflow.com/questions/2445330/importance-of-a-singlecolon-in-c

我无法弄清楚示例构造函数初始值设定项中的元素是什么,因为它们不是类IOobject的成员变量,也不是派生类的构造函数。有人能告诉我这些初始化元素的用途是什么吗?

问候直播

2 个答案:

答案 0 :(得分:2)

IOobject类定义了以下成员(头文件):

127  //- Name
128  word name_;
129 
130  //- Class name read from header
131  word headerClassName_;
132 
133  //- Optional note
134  string note_;
135 
136  //- Instance path component
137  fileName instance_;
138 
139  //- Local path component
140  fileName local_;
141 
142  //- objectRegistry reference
143  const objectRegistry& db_;
144 
145  //- Read option
146  readOption rOpt_;
147 
148  //- Write option
149  writeOption wOpt_;
150 
151  //- Register object created from this IOobject with registry if true
152  bool registerObject_;
153 
154  //- IOobject state
155  objectState objState_;

在这里查看标题文件:http://foam.sourceforge.net/docs/cpp/a06519_source.html

,这是构造函数签名

IOobject    
(
    const word &        name,
    const word &        instance,
    const fileName &        local,
    const objectRegistry &      registry,
    readOption          r = NO_READ,
    writeOption         w = NO_WRITE,
    bool            registerObject = true
)

并且有可用的文档here

如果您需要更多详细信息,请告诉我,我会尝试为您寻找更多信息。

答案 1 :(得分:2)

从您的链接,如果您导航到头文件,它会显示成员,因此很明显没有基类,构造函数中的初始化列表会单独初始化成员。

127  //- Name
128  word name_;
129 
130  //- Class name read from header
131  word headerClassName_;
132 
133  //- Optional note
134  string note_;
135 
136  //- Instance path component
137  fileName instance_;
138 
139  //- Local path component
140  fileName local_;
141 
142  //- objectRegistry reference
143  const objectRegistry& db_;
144 
145  //- Read option
146  readOption rOpt_;
147 
148  //- Write option
149  writeOption wOpt_;
150 
151  //- Register object created from this IOobject with registry if true
152  bool registerObject_;
153 
154  //- IOobject state
155  objectState objState_;