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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:17:44+00:00 2026-05-25T17:17:44+00:00

I have a header file that has some static variables for all of my

  • 0

I have a header file that has some static variables for all of my files to use. I have a boolean variable in there initialized to 0 –

//in utility.h
static bool read_mess = false;

that I want to change to true if –view-read-messages is in the command line arguments so that I can do something like this when I get a message from a client –

//code from a different file
if(UTILITY_H::read_mess)
    std::cout<<"\nMessage successfully received from Client 2: "<<in2;

In main, I check for the command line argument and set the variable, read_mess, to true –

//this is in a for, where temp is the command line arg[i]
else if(strcmp(temp.c_str(), "--view-read-messages") == 0) {
    UTILITY_H::read_mess = true;
}

I can print the value of read_mess after this line in main and it says that it is true. But when I’m checking if its true in the if statement I posted above, read_mess is back to false. Why does this happen? I’m sure its just something simple, but I can’t seem to make it work. Are all of the variables in utility.h reinitialized each time I do UTILITY_H::? And if so, why?

  • 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-25T17:17:44+00:00Added an answer on May 25, 2026 at 5:17 pm

    static in this context means “local” (to the translation unit). There will be multiple copies of read_mess in your program, one per translation unit which is not the same thing as a header file. (In your case you can most likely approximate “translation unit” as .cpp or .c or .cc file).

    Probably what you meant to do was to declare an extern variable, or static class member and define it in just one translation unit.

    In practice using extern means in your header file you want to write:

    extern bool read_mess;
    

    But in one and only one other place that isn’t a header:

    bool read_mess = false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a header file, lets say Common.h, that is included in all of
I have a header file variable.h where i declare all my global variable.Then i
If I have a text file that has a header record and field delimited
I have a .cpp file which has some static free functions. I know how
I have a c++ implementation file (my.cpp) that indirectly includes a header file (b.h)
I have a file called header.php that I am including on every page on
I have a header file buildTree.h and a C file buildTree.c there is a
I have a header file with all the enums listed (#ifndef #define #endif construct
I have this header file, zeeheader.h, and I wrote some classes in it, I'm
I have a header file. In this header I want to use a map

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.