什么是静态构造函数?

这个问题在采访中被问到:

什么是静态构造函数?

它存在于C ++吗? 如果是,请举例说明。

C ++没有静态构造函数,但可以使用嵌套类的静态实例来模拟它们。

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; 

在C ++中,没有静态构造函数。 在C#中(也可能在Java中),可以定义由运行时自动调用的静态构造函数,以初始化静态成员。

对于进一步的问题和兴趣,你可以阅读这个话题:

在C ++中没有静态构造函数的基本原理是什么?

由于我们在技术上没有C ++中的静态构造函数,因此您必须决定是否值得做一些棘手的事情(例如使用嵌套类的静态实例),或者稍微调整代码以调用静态初始化器在你的程序的生命早期。

 #include <iostream> // cout, endl class Foo { public: static int s_count; // Constructor definition Foo (int l, int w, int h) { cout <<"Foo ctor called." << endl; length = l; width = w; height = h; // Increase every time object is created s_count++; } int vol () { return length * width * height; } static void initCount() { s_count = 0; } static int getCount() { return s_count; } private: double length; // Length of a box double width; // Width of a box double height; // Height of a box }; // Initialize static member of class Foo int Foo::s_count; // Initializing here is non-deterministic int main(void) { Foo::initCount(); // Initializing here is deterministic // Print total number of objects before creating object. cout << "Inital Count: " << Foo::getCount() << endl; Foo Foo1(3, 1, 1); // Declare box1 Foo Foo2(8, 6, 2); // Declare box2 // Print total number of objects after creating object. cout << "Final Count: " << Foo::getCount() << endl; return 0; } Output: $ static_init_test Inital Count: 0 Foo ctor called. Foo ctor called. Final Count: 2 

我更喜欢这种方法; 作为一线希望,它将非确定性初始化带出。

尽pipe有一个问题 – 如果你试图初始化静态常量variables,这种技术是不够的。 对于静态常量variables,你必须使它们对类是私有的,并为外人提供getter来读取它们。

注意:我更新了这个代码 – 它编译和运行成功,没有警告通过:

 g++ static_init_test.cpp -std=c++11 -o static_init_test 

静态构造函数存在于C#和Java中。
它们被用来初始化一个类的静态成员。
运行时在第一次使用类之前执行它们。

C ++中没有这样的事情。 构造函数和destrcutors通常用于创build或破坏对象的实例。 调用它们没有相应的对象实例是没有意义的。 你可以使用单例模式来模拟它们。

可能是他们的意思是:

 class Cat { private: Cat(); public: static Cat getCat() {return Cat(); } } 

静态构造函数用于初始化类的静态数据。 C ++没有静态构造函数。 但是一个静态的构造函数可以通过使用下面的朋友类或嵌套类来模拟。

 class ClassStatic{ private: static char *str; public: char* get_str() { return str; } void set_str(char *s) { str = s; } // A nested class, which used as static constructor static class ClassInit{ public: ClassInit(int size){ // Static constructor definition str = new char[size]; str = "How are you?"; } } initializer; }; // Static variable creation char* ClassStatic::str; // Static constructor call ClassStatic::ClassInit ClassStatic::initializer(20); int main() { ClassStatic a; ClassStatic b; std::cout << "String in a: " << a.get_str() << std::endl; std::cout << "String in b: " << b.get_str() << std::endl; a.set_str("I am fine"); std::cout << "String in a: " << a.get_str() << std::endl; std::cout << "String in b: " << b.get_str() << std::endl; std::cin.ignore(); } 

输出:

 String in a: How are you? String in b: How are you? String in a: I am fine String in b: I am fine 

在C ++中,不存在静态构造函数。

模拟静态构造函数行为的其他方法是使用具有私有构造函数和静态工厂方法的实例variables。

 Cat* Cat::give_birth() { static Cat *myone = NULL; if (myone == NULL) { myone = new Cat(); } return myone; }