继承的Constructor代码什么时候被调用
如果我的类的构造函数继承了另一个类的参数化构造函数,那么该继承的构造函数代码会在我放置在构造函数中的代码之前还是之后执行?
例如:
TCurrentKillerThread::TCurrentKillerThread() : TThread(true){
CurrentKillerMutex = CreateMutex(NULL,true,NULL); // protect thread
try {
Write("Created Current Killer");
} __finally {
ReleaseMutex(CurrentKillerMutex);
}
Start();
}
TThread(true)
会在 TCurrectKillerThread()
中的代码之前执行吗?
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:
TCurrentKillerThread::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()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。父类总是在派生类之前初始化。不过,您并没有继承构造函数 - 您正在调用它。
Yes. The parent class is always initialized before the derived. You are not inheriting the constructor though - you're calling it.
是的,C++ 构造是从最低的基类到最派生的类进行的。
Yes, C++ construction works from the lowest base class to the most derived one.
是的,当派生类对象实例化时,
TThread(bool var){ .... }
在TCurrentKillerThread(){ .... }
之前执行。在C++中,父类子对象的构造必须发生在派生类子对象之前。Yes,
TThread(bool var){ .... }
is executed beforeTCurrentKillerThread(){ .... }
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