开发者

When is the inherited Constructor code called

开发者 https://www.devze.com 2023-02-26 14:04 出处:网络
If the constructor of my class inherits a parametrized constructor of another class, will that inherited constructor code be executed before or after the code that I place in my constructor?

If the constructor of my class inherits a parametrized constructor of another class, will that inherited constructor code be executed before or after the code that I place in my constructor?

For instance in this:

TCurrentKillerTh开发者_运维问答read::TCurrentKillerThread() : TThread(true){
  CurrentKillerMutex = CreateMutex(NULL,true,NULL); // protect thread 
  try {
     Write("Created Current Killer");
  } __finally {
    ReleaseMutex(CurrentKillerMutex);
  }
  Start();
}

Would TThread(true) be executed before the code I have in TCurrectKillerThread()?


Yes. The parent class is always initialized before the derived. You are not inheriting the constructor though - you're calling it.


Yes, C++ construction works from the lowest base class to the most derived one.


Would TThread(true) be executed before the code I have in TCurrectKillerThread()?

Yes, TThread(bool var){ .... } is executed before TCurrentKillerThread(){ .... } when the derived class object is instantiated. Construction of parent class sub-object must take place before the derived class sub-object in C++.


Of course, the constructor of base class is executed before the constructor of subclass, and destructor is executed from subclass to base class

0

精彩评论

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

关注公众号