问:类静态成员能否在类的构造函数/类的非静态函数中初始化?
答:不能
class requestData
{public:requestData(int fd_,int epoll_fd_);~requestData();static std::unordered_map<std::string,std::string> header_map;static int d;
};
requestData::requestData(int fd_,int epoll_fd_)
{header_map={{"first","1"},{"second","2"}};d = 0;
}
会报错d和header_map未定义的引用。感觉这个就是编程语言的限定,考虑到类静态成员是全局变量,所以不允许在类中初始化。
问:类静态成员能否在声明时直接定义?
答:不能
class requestData
{public:requestData(int fd_,int epoll_fd_);~requestData();static std::unordered_map<std::string,std::string> header_map={{"first","1"},{"second","2"}};static int d=0;
};
C++ forbids in-class initialization of non-const static member