是否可以动态初始化静态成员数组
关于C++中初始化静态成员的问题有很多,但我找不到这个。
class Node {
private:
static const int INITIAL_SIZE = 100;
static Node* node_space;
static int OUT_OF_BOUNDS = 0;
static Node BAD_NODE;
};
Node* Node::node_space = new Node[Node::INITIAL_SIZE];
这似乎可行,但我还想将 BAD_NODE 添加到该数组作为第一个元素。
Node Node::BAD_NODE = Node();
Node::node_space[OUT_OF_BOUNDS] = BAD_NODE;
上面的内容无法编译。消息是
Node.cpp:7: error: expected constructor, destructor, or type conversion before '=' token
这是一个学校项目,我们正在其中实现一个带有数组的链表。
There are so many questions about initializing static members in C++, and yet I couldn't find this one.
class Node {
private:
static const int INITIAL_SIZE = 100;
static Node* node_space;
static int OUT_OF_BOUNDS = 0;
static Node BAD_NODE;
};
Node* Node::node_space = new Node[Node::INITIAL_SIZE];
This seems to work, but I also want to add BAD_NODE to this array as the first element.
Node Node::BAD_NODE = Node();
Node::node_space[OUT_OF_BOUNDS] = BAD_NODE;
The above doesn't compile. The message is
Node.cpp:7: error: expected constructor, destructor, or type conversion before '=' token
This is for a school project in which we are implementing a linked list with an array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只有一个静态数据对象,但您想动态初始化它,那么您可能想要在这里做的是创建一个单例对象作为 Node 类的包装器。基本上,单例会发生的情况是,您创建一个使用普通类构造函数初始化的类的单个版本,但构造函数、operator=() 和复制构造函数被声明为私有。然后通过静态变量创建该类的单个静态版本,并且有一个公共访问器方法允许代码的其他部分访问该单例类(即访问器返回对静态类的引用或常量引用)你创建的)。
现在您可以通过
Node_S::get_instance()
访问您的单例。由于复制和赋值运算符被声明为私有
,因此您无法创建单例的额外副本...只会创建此类的单个实例。如果您需要传递它,您可以通过引用来传递。此外,不存在初始化歧义,因为当调用get_instance()
时,Node
的所有静态元素都会在运行时按顺序初始化。由于singleton_instance
是一个静态变量,构造函数Node_S()
运行的次数就只有一次,所以你基本上可以把 Node 的所有初始化代码都放在 安全地位于构造函数内部。然后只需在Node_S
接口中添加使用Node
类型所需的任何其他方法即可。所以一些常见的用法代码可能如下所示:What you may want to-do here, if you only have a single static data-object, but you want to dynamically initialize it, is to create a singleton object as a wrapper around your
Node
class. Basically what occurs with a singleton is you create a single version of a class that is initialized with a normal class constructor, but the constructor,operator=()
, and copy-constructor are declaredprivate
. Then a single static version of the class is created through a static variable, and there is a public accessor method that allows other portions of your code to access the singleton class (i.e., the accessor returns either a reference or constant reference to the static class you created).Now you would access your singleton via
Node_S::get_instance()
. Since the copy and assignment operators are declaredprivate
, you cannot create extra copies of your singleton ... there will only be a single instance of this class created. If you needed to pass it around, you would do-so by reference. Furthermore there is no initialization ambiguity because all the static elements ofNode
are initialized in-order during run-time whenget_instance()
is called. Sincesingleton_instance
is a static variable, the number of times the constructorNode_S()
is run is only once, so you can basically place all your initialization code forNode
safely inside of the constructor. Then simply add any additional methods required to work with theNode
type in yourNode_S
interface. So some common usage code might look like the following:在 C++11 中(即“在 C++ 中”:-)),您可以为数组提供初始化列表:
只有当您打算将 first 元素设置为特定内容时,这才有效。初始化列表必须提供元素的初始序列,因此不清楚如果 OUT_OF_BOUNDS 不为零,这些元素会是什么。
In C++11 (i.e "in C++" :-)), you can provide an initialization list for arrays:
This will only work if you mean to set the first element to something specific. The initialization list has to provide the initial sequence of elements, so it's not clear what those would be if
OUT_OF_BOUNDS
wasn't zero.