static-members

How to initialize private static members in C++?

痴心易碎 提交于 2019-11-25 22:18:19
问题 What is the best way to initialize a private, static data member in C++? I tried this in my header file, but it gives me weird linker errors: class foo { private: static int i; }; int foo::i = 0; I\'m guessing this is because I can\'t initialize a private member from outside the class. So what\'s the best way to do this? 回答1: The class declaration should be in the header file (Or in the source file if not shared). File: foo.h class foo { private: static int i; }; But the initialization should