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 8233279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:04:35+00:00 2026-06-07T18:04:35+00:00

With simple values, there is no problem: we simply set them in the .cpp

  • 0

With simple values, there is no problem: we simply set them in the .cpp file.

But consider the following example, that require some init/cleanup code:

// .h
class MyClass {
...
private:
    static unsigned First1000Primes[1000];
} ;

In the .cpp file, we want to initialize the array, and need some code for that.

We can define a class Initializer in the .cpp file, with a ctor that fills the values, and declare a static Initializer InitAndCleanup in the same file, so the code will run once at startup (and similarly run dtor/cleanup code if needed).

The problem with this solution is its ‘messiness’, as the initializer has to be a friend of MyClass to access its private parts.

So my question is: is there a cleaner way?

  • 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-06-07T18:04:36+00:00Added an answer on June 7, 2026 at 6:04 pm

    I would say that like everything else, it’s just a matter of encapsulation and packaging.

    struct PrimeArray {
        PrimeArray();   // init here
        PrimeArray(PrimeArray const&) = delete;
        PrimeArray& operator=(PrimeArray const&) = delete;
        ~PrimeArray();  // cleanup here
    
        unsigned data[1000];
    };
    

    And then use that:

    class MyClass {
    public:
        // ...
    private:
        static PrimeArray const primes;
    };
    

    And initialize in the cpp as usual:

    PrimeArray const MyClass::primes{};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a simple way to change the default error values in the jQuery
My question is straight forward and simple, Like there is layout-sw600dp,values-sw600dp, for layout and
Possible Duplicate: A simple program to CRUD node and node values of xml file
I want to post some values from a simple HTML form, validate them with
Consider a nonlinear least squares model in R, for example of the following form):
I'm creating a simple BizTalk 2010 process that will convert a 4010 835 file
I have a simple html form that submits information with POST function. But when
(I know there's some close duplicates out there, but none of them were able
I'm a novice programmer. This may be a simple problem but I've never seen
I have a fairly simple problem. I have a select dropdown with values 1

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.