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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:30:24+00:00 2026-06-13T03:30:24+00:00

I am trying to create bunch of void methods and call them later in

  • 0

I am trying to create bunch of void methods and call them later in program.
I will demonstrate the code ahead to help better understand my issue.

.h file
static float sfloat;
namespace someNamespace
{
static void foo();
}
.cpp file
void someNamespace::foo(){cout<<sfloat<<endl}
  • above code is simpler version of class that I was working on.

I initialize sfloat in other .cpp file

otherFile.cpp
void initializeAndUseFoo(){sfloat = 5; someNamespace::foo();}

As far as my understanding goes, I expect foo to print out 5 but it prints out 0 instead.
This behavior occurs across all other static variables that I have as well(pointer included). It seemed that somehow the variables inside the function are never initialized as the value I assign.

However if I call out “sfloat” not through the function then I can call it out properly.(if I just print it out on console with just

cout<<"just print it not through the function : " <<sfloat<<endl;

then it is indeed 5

Thank you for reading.

  • 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-13T03:30:25+00:00Added an answer on June 13, 2026 at 3:30 am

    A global static variable is static within one compilation unit. If you make another compilation unit, it will have a separate copy of that static variable.

    This SO question explains what a compilation unit is in C++.

    Since your variable is static and global in the same time, there will be a single instance of it in it’s compilation unit. If you want to access that exact variable from another file, you will have to use the extern keyword. Without it, a new copy of that variable will be created in each compilation unit. You can find some information here, for example.

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

Sidebar

Related Questions

I am trying to create a bunch of dynamic helper methods like these: show_admin_sidebar
I'm trying to find a way to create a nice windows form that will
I'm trying to dynamically create a bunch of class properties, but each dynamic fget
I am trying to use the output from mdfind to create a bunch of
I'm trying to create an application icon. I took a bunch of created *.ico
I am trying to run a program that analyzes a bunch of text files
I am trying to create a GTK toolbar with a bunch of items. My
SO I am trying to create a screen that will display about 50 toggle
I'm trying to create a MPG4 movie from a bunch of png's. I want
Basically, I'm trying to create an application that features a bunch of colored rectangles

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.