是否可以动态初始化静态成员数组

发布于 2024-12-04 18:49:37 字数 593 浏览 3 评论 0原文

关于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隱形的亼 2024-12-11 18:49:37

如果您只有一个静态数据对象,但您想动态初始化它,那么您可能想要在这里做的是创建一个单例对象作为 Node 类的包装器。基本上,单例会发生的情况是,您创建一个使用普通类构造函数初始化的类的单个版本,但构造函数、operator=() 和复制构造函数被声明为私有。然后通过静态变量创建该类的单个静态版本,并且有一个公共访问器方法允许代码的其他部分访问该单例类(即访问器返回对静态类的引用或常量引用)你创建的)。

class Node_S
{
    private:

        //your original Node class we're wrapping in the singleton object
        struct Node {
            static const int INITIAL_SIZE = 100;
            static Node* node_space;
            static int OUT_OF_BOUNDS;
            static Node BAD_NODE;
        };

        //private default constructor is only called once
        Node_S()
        {
            //place your original initialization code for Node here
            Node::OUT_OF_BOUNDS = 0;
            Node::node_space = new Node[Node::INITIAL_SIZE];
            Node::BAD_NODE = Node();
            Node::node_space[Node::OUT_OF_BOUNDS] = Node::BAD_NODE;
        }

        //private copy and assignment operator
        Node_S(const Node_S&) {}
        Node_S& operator=(const Node_S&) { return *this; }

    public:

        static Node_S& get_instance() 
        {
            //here is where the single version of Node_S is created
            //at runtime
            static Node_S singleton_instance = Node_S();
            return singleton_instance;
        }

        //... Other public functions
};

现在您可以通过 Node_S::get_instance() 访问您的单例。由于复制和赋值运算符被声明为私有,因此您无法创建单例的额外副本...只会创建此类的单个实例。如果您需要传递它,您可以通过引用来传递。此外,不存在初始化歧义,因为当调用 get_instance() 时,Node 的所有静态元素都会在运行时按顺序初始化。由于 singleton_instance 是一个静态变量,构造函数 Node_S() 运行的次数就只有一次,所以你基本上可以把 Node 的所有初始化代码都放在 安全地位于构造函数内部。然后只需在 Node_S 接口中添加使用 Node 类型所需的任何其他方法即可。所以一些常见的用法代码可能如下所示:

Node_S::Node a_node_copy = Node_S::get_instance().get_node(10);

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 declared private. 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).

class Node_S
{
    private:

        //your original Node class we're wrapping in the singleton object
        struct Node {
            static const int INITIAL_SIZE = 100;
            static Node* node_space;
            static int OUT_OF_BOUNDS;
            static Node BAD_NODE;
        };

        //private default constructor is only called once
        Node_S()
        {
            //place your original initialization code for Node here
            Node::OUT_OF_BOUNDS = 0;
            Node::node_space = new Node[Node::INITIAL_SIZE];
            Node::BAD_NODE = Node();
            Node::node_space[Node::OUT_OF_BOUNDS] = Node::BAD_NODE;
        }

        //private copy and assignment operator
        Node_S(const Node_S&) {}
        Node_S& operator=(const Node_S&) { return *this; }

    public:

        static Node_S& get_instance() 
        {
            //here is where the single version of Node_S is created
            //at runtime
            static Node_S singleton_instance = Node_S();
            return singleton_instance;
        }

        //... Other public functions
};

Now you would access your singleton via Node_S::get_instance(). Since the copy and assignment operators are declared private, 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 of Node are initialized in-order during run-time when get_instance() is called. Since singleton_instance is a static variable, the number of times the constructor Node_S() is run is only once, so you can basically place all your initialization code for Node safely inside of the constructor. Then simply add any additional methods required to work with the Node type in your Node_S interface. So some common usage code might look like the following:

Node_S::Node a_node_copy = Node_S::get_instance().get_node(10);
咿呀咿呀哟 2024-12-11 18:49:37

在 C++11 中(即“在 C++ 中”:-)),您可以为数组提供初始化列表:

Node* Node::node_space = new Node[Node::INITIAL_SIZE] { Node::BAD_NODE };

只有当您打算将 first 元素设置为特定内容时,这才有效。初始化列表必须提供元素的初始序列,因此不清楚如果 OUT_OF_BOUNDS 不为零,这些元素会是什么。

In C++11 (i.e "in C++" :-)), you can provide an initialization list for arrays:

Node* Node::node_space = new Node[Node::INITIAL_SIZE] { Node::BAD_NODE };

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文