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

  • Home
  • SEARCH
  • 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 6373987
In Process

The Archive Base Latest Questions

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

In C++ I have an array of doubles which need initialising programmatically, at run

  • 0

In C++ I have an array of doubles which need initialising programmatically, at run time, just once, for the whole class to share. They could be both static and constant. How best can I initialise them? I have gone off using static with the prodigious popularity of parrallel processors presently pervading. So must I set a flag to run once or is there some static const magic which will initialise them as a variable local to a function (ok) or class (ok too)?

    double sumOfWeights = 0.0;
    double fracObs = 0.0;
    for (int i = 0; i < NUMTRACES; i++) {
        double weightAtI = SQUARED(1 - SQUARED(MAXTRACEWRTMIDTRACE * (MIDTRACE - i)
                                        / double(MIDTRACE)));
        sumOfWeights += weightAtI;
        fracObs += obsArray[i] * weightAtI;
    }
    return fracObs / sumOfWeights;

In the code above I’d like to make weightAtI a lookup with each double already divided by sumOfWeights so I can retrieve them without iterating through NUMTRACES.

_EDIT_
It’s okay, that’s what constructors are for 🙂
Just hoping to tackle my static, const and initialization gremlins into the bargain. Thanks Seth

_EDIT_
Not sure it is quite the effect I wanted though. The constructor runs on each instance, even if the members are static, no? No. Lemme see…

_EDIT_
I think the most efficient solution, is to guard the initializer loop with a static flag, in the constructor. Being a POD flag I’m sure it should behave appropriately, I’m just not quite sure what that is at this stage.

_EDIT_
Ahh, got it:

class X
{
public:
    static int i;
};
int X::i = 0; // definition outside class declaration

_EDIT_
Unfortunately, when it comes to my code,

    static const int MIDTRACE = 3;
    static const int NUMTRACES = 2 * MIDTRACE + 1;
    static double WEIGHTATI[NUMTRACES];

I get linker errors:

meobj.obj : error LNK2020: unresolved token (0A00001C) “private: static double * mens:meclass::PIMPL::WEIGHTATI” (?WEIGHTATI@PIMPL@meclass@mens@@$$Q0PANA)
meobj.obj : error LNK2001: unresolved external symbol “private: static double * mens:meclass::PIMPL::WEIGHTATI” (?WEIGHTATI@PIMPL@meclass@mens@@$$Q0PANA)

due to my constructor:

meclass::PIMPL() {
    if (!doneStaticInit) {
        double sumOfWeights = 0.0;
        for (int i = 0; i < NUMTRACES; i++) {
            WEIGHTATI[i] = SQUARED(1 - SQUARED(MAXTRACEWRTMIDTRACE * (MIDTRACE - i) / double(MIDTRACE)));
            sumOfWeights += WEIGHTATI[i];
        }
        for (int i = 0; i < NUMTRACES; i++) WEIGHTATI[i] /= sumOfWeights;
        doneStaticInit = true;
    }
}
  • 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-25T01:25:58+00:00Added an answer on May 25, 2026 at 1:25 am

    You can put a static bool flag in your constructor. The flag will only be initialized to false the first time it is called. After that it will remain true.

    // foo.h
    
    class Foo {
      static const int MIDTRACE = 3; // static const, no definition needed
      static const int NUMTRACES = 2 * MIDTRACE + 1; // static const, no definition needed
      static double WEIGHTATI[NUMTRACES]; // not const, so need definition outside of class
    public:
      Foo() {
        static bool array_initialized = false;
        if( !array_initialized ) {
          // Initialize array
          array_initialized = true;
        }
      }
      // Other members
    };
    

    In a source file, not header file:

    // foo.cpp
    include "foo.h"
    double Foo::WEIGHTATI[NUMTRACES];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of doubles and I need to create another array which
I have a Java method which returns an array of doubles. I would then
I have defined a class in C++ which holds an array of scalars of
I have an algorithm which currently allocates a very large array of doubles, which
I have a 2D array of doubles in Java which is basically a table
I have a json_encoded array which is fine. I need to strip the double-quotes
I have an array of doubles and I want the index of the highest
I have to load an array of doubles from a file, multiply each element
I have an array of double pointers, but every time I try do print
I have a very large array of doubles that I am using a disk-based

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.