Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:03:57+00:00 2026-05-25T15:03:57+00:00

There are so many questions about initializing static members in C++, and yet I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:03:58+00:00Added an answer on May 25, 2026 at 3:03 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are already so many questions asked about this already. One popular answer is
I know there are multiple questions about this same question but I couldn't find
There are many questions about this subject , but none (except one but still
There seem to be many questions asked about this subject here on stackoverflow, but
I know, there are many different questions and so many answers about this problem...
I know there are many questions and answers about this, but I am looking
I know there are many questions about this but none of them helped my
ok I am well aware there are many other questions about this, but I
There are many questions on this site about return value optimization (I suppose it's
I know there exists many questions about this, but I dont know what to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.