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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:29:37+00:00 2026-05-18T11:29:37+00:00

I was wondering why sometimes my global const defined in seperate .h file isn’t

  • 0

I was wondering why sometimes my global const defined in seperate .h file isn’t properly initialized when I need it. Some tests lead me to situation that I can’t understand. I don’t know how to explain it, so here’s the code:

main.cpp

#include <iostream>
#include "class.h"
using namespace std;

A a;
B b;

int main(int argc, char* argv[]){
A aa;
B bb;
cout<<a.a<<" "<<aa.a<<endl;
cout<<b.b<<" "<<bb.b<<endl;
return 0;
}

class.h

#ifndef CLASS_H
#define CLASS_H
#include "const.h"

class A {
public:
A();
float a;
};

class B {
public:
B():b(CONST){}
float b;
};
#endif

class.cpp

#include "class.h"
A::A()
: a(CONST){}

const.h

#ifndef CONST_H
#define CONST_H
#include <limits>
using namespace std;

const float CONST = numeric_limits<float>::has_infinity ? 
        -numeric_limits<float>::infinity() : 
        -numeric_limits<float>::max();
#endif

After running above code I get:

0 -1.#INF
-1.#INF -1.#INF

when actually I would like to get 4 times ‘-1.#INF’.
Why does it happen this way? If CONST would be ‘1’ instead of above formula, it would work perfectly.

I can “fix” it by making static getConst() method:

static float getConst(){
static const float CONST = numeric_limits<float>::has_infinity ? 
        -numeric_limits<float>::infinity() : 
        -numeric_limits<float>::max();
return CONST;}

but it just doesn’t “feel” right. On other hand I just need two of those above… But maybe there’s some other way?

And, most importantly, why class B gets “right” CONST and class A don’t?

  • 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-18T11:29:38+00:00Added an answer on May 18, 2026 at 11:29 am

    Constant objects have internal linkage, so you get a separate copy of CONST in each compilation unit that includes const.h. In this case, you’ll get one in main.cpp, and one in class.cpp. You also have your two global objects in main.cpp.

    C++ does not define the order in which global objects in different compilation units are initialised. As constructor is called from main.cpp, and needs the global constant from class.cpp, which may not yet be initialised. In your particular case, it hasn’t been, so you get the incorrect value you saw. Bs constructor is inline, so it uses the global constant from main.cpp, which has been initialised since it is defined earlier in the same compilation unit.

    By making the constant a function-static object, it is now guaranteed to be initialised when that function is first called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if I need to do something in my swf to be able to
I was just wondering what was the most efficient way of adding lots (sometimes
I am sending a few jpegs (and sometimes zip) files. I was wondering if
so sometimes my html needs some special classes/div's/attributes/.. for the jquery scripts to work
Sometimes I need to provide a Context object to call specific functions, such as
I've been a Delphi (D7) developer for many sometime already, I've always been wondering
Wondering if anyone has gotten the infamous database is locked error from Trac and
Wondering if there is any Text to Speech software available as a plug in
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.
Wondering how to open many new windows with Javascript. I have found plenty of

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.