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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:51:51+00:00 2026-05-20T23:51:51+00:00

I am looking at some code in a company I am currently working at

  • 0

I am looking at some code in a company I am currently working at and I see a few (not a lot) declarations of static global variables in the *.cpp files (for example, to store a list of listeners) where the .h/.cpp files belong to a class. If a variable (static or otherwise) that is used only by class itself, I always declare it as private.

Is there an advantage to this over declaring the variable private? Is this bad practice? Or is this normal when declaring static variables that are used by the class only and nobody else?

EDIT: In my question I asked about static, but what if it is a non-static global variable in the .cpp file instead of it being a private member of the class? Is that bad practice or considered ok? Any advantages in this case?

  • 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-20T23:51:52+00:00Added an answer on May 20, 2026 at 11:51 pm

    The main advantage to this way is reducing the amount of “unnecessary” stuff in the *.h file. This might slightly improve compile times and/or rebuild complexity when files or modified, and it may make the header file slightly easier to read.

    (In my opinion, these advantages are small, and I will usually prefer the clarity of putting things that logically are related to a class in that class’s scope.)

    However, static global variables are deprecated and bad practice in C++. If there is no other appropriate scope, an anonymous namespace should be used.

    // Instead of this:
    static std::list<MyClass*> MyClass_population;
    
    // Do this:
    namespace { // anonymous
        std::list<MyClass*> MyClass_population;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking at some code I'm maintaining in System Verilog I see some signals that
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I am looking at some code and it has this statement: ~ConnectionManager() { Dispose(false);
Today I was looking at some code atually from Nikhil Kothari's Facebook.NET API. I
OK so I'm looking a some code which looks roughly like this: void DoSomething(object
Looking for some sample code (C#) for a simple thread pool implementation. I found
I'm looking for some sample code that will sort the list items in an
I'm looking at some GXT code for GWT and I ran across this use
I am looking at some javascript code and it has this in a function:
I am looking to write some C# code for linux/windows/mac/any other platform, and am

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.