Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个关于如何为类中的静态变量动态分配内存的问题。考虑这个例子
class my_class { static int *ptr_to_int; // Don't care };
如何为 ptr_to_int 分配内存?
在单个实现文件中:
int* my_class::ptr_to_int = new int;
但是为什么?你真的需要指针吗?只是有什么问题
class my_class { static int actual_int; // Don't care };
此处的类名仅用作您的目的的范围名称。只需使用 my_class::ptr_to_int 作为静态指针。