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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:33:59+00:00 2026-05-31T08:33:59+00:00

What the difference between a static member variable of a class and a free

  • 0

What the difference between a static member variable of a class and a “free” variable defined in the “body” of the class? I mean can I use the second in place of the first to discharge a header of the class?

// b.h
class B
{
public:
  B(int j);
  void print();
private:
  static int is;
};


//b.cpp
#include <iostream>

int i = 0;
int B::is = 0;

B::B(int j)
{
  i = j;
  is = j;
}

void B::print(){
  std::cout << i << " " << is << " " << std::endl;
}

//main.cpp
int main() {
  B b1(1);
  b1.print();
  B b2(2);
  b2.print();
  b1.print();
  return 0;
}

output:

1 1 
2 2 
2 2 
  • 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-31T08:34:00+00:00Added an answer on May 31, 2026 at 8:34 am

    In your case, the difference is that the static member data can be accessed only by the class, as it is declared in the private section of the class, which may be desirable in some cases. The global i can be changed by anyone.

    So if a variable is meant for the class only, and shouldn’t be used by others, then having a static member data in the private section is a better solution. It gives you restricted access to the variable. Global variables are bad in general, as it gives unrestricted access to anyone.

    Note that in your case, the global variable i has external linkage. In other files, one could simply declare as extern int i, and then can use i and modify it as well.

    Even if you make it static global variable, to make it have internal linkage, it is not good as compared to static member data, because the static member data gives you some sort of idea that the variable is used by the class only, not by non-class members (if any in the same file). So static member data increases readability, in comparison to global static data.

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

Sidebar

Related Questions

Possible Duplicate: Difference between static class and singleton pattern? we use static class for
I would like to know what is the difference between initializing a static member
What is the difference between static and non-static nested class?
What real (i.e. practical) difference exists between a static class and a singleton pattern?
Is there any difference between a variable declared as static outside any function between
What is the main difference between an inner class and a static nested class
What is the difference between static and shared libraries? I use Eclipse and there
What is the difference between a static member function and an extern C linkage
When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between
Is there any difference between a Singleton class and a class with all static

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.