什么是静态构造函数? [英] What is a static constructor?
本文介绍了什么是静态构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题是在采访中问我的:
This question was asked to me in an interview:
什么是静态构造函数?
What is a static constructor?
它在C ++中存在吗?如果是,请举例说明。
Does it exist in C++? If yes, please explain it with an example.
推荐答案
C ++没有静态构造函数,但您可以使用静态构造函数进行仿真
C++ doesn’t have static constructors but you can emulate them using a static instance of a nested class.
class has_static_constructor {
friend class constructor;
struct constructor {
constructor() { /* do some constructing here … */ }
};
static constructor cons;
};
// C++ needs to define static members externally.
has_static_constructor::constructor has_static_constructor::cons;
这篇关于什么是静态构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文