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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:59:32+00:00 2026-05-13T17:59:32+00:00

I have a few questions about the static keyword in C++ (and probably with

  • 0

I have a few questions about the static keyword in C++ (and probably with other languages as well.) What is the purpose of declaring a function as static?

void static foo(int aNumber) {
... }

How about a static inline function?

void static inline foo(int aNumber) {
... }

Is there any benefit to using the static keyword with a function, and do those benefits apply to class functions as well?
I realize some datatypes like structs and arrays have to be static when compiling with an older compiler, but is there any point when using a new ANSI-C++ compiler (like MS VC++ 2008)? I know that using a static variable inside a loop saves time by keeping the data in memory and not reallocating memory every loop iteration, but how about when a variable is declared only once, like at the top of a header file or within a namespace?

  • 1 1 Answer
  • 4 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-13T17:59:32+00:00Added an answer on May 13, 2026 at 5:59 pm

    Depends on Context:

    Like many things in C++, static means different things depending on its context.

    It’s very common in C++ for the same word to mean different things depending on its context.
    For example:

    • * is used for multiplication, dereferencing a pointer, and creating pointers.
    • & is used to get the address of variables, to declare a reference, and as a bitwise AND operator.

    Global use of static:

    If you declare a function or variable as static outside of a class and in global scope, it is specific to only that file. If you try to use that variable or function in a different file (via a forward declaration) you will get a linking error.

    Example:

    a.cpp:

    static void fn()
    {
      cout<<"hello a!"<<endl;
    }
    

    b.cpp:

    void fn();
    void gn()
    {
      fn();//causes linking error
    }
    

    This feature allows you to use a function that no other file will ever see, that way you don’t cause possible linker errors of a symbol defined multiple times. The preferred method to do this is with anonymous namespaces though:

    a.cpp:

    namespace
    {
      void fn() // will be static to a.cpp
      {
        cout<<"hello a!"<<endl;
      }
    }
    

    Inside of a class use of static:

    If you declare a function or variable as static inside of a class (or struct), it is a class function or class variable. This means that there is only one for that whole class. A class function can only use class variables. A class variable is shared amongst all instances of that class.

    class C
    {
    public:
      static void fn()
      {
        y = 4;//<--- compiling error
        // can't access member variable within a static function.
      }
    
      int y;
    }
    

    This is a great feature to use if you have something that is specific to the class of your objects, but not specific to an instance.

    Inside a function use of static:

    If you declare a variable as static inside of a function, you can consider that the variable value will persist upon calls. It will only be initialized once.

    Example:

    //Will print 0, then 1, then 2, ...
    void persistentPrintX()
    {
      static int x = 0;
      cout << x << endl;
      x++;
    }
    

    I personally try to avoid this, and you probably should to. It is not good to have global state. It is better to have functions that given the same input guarantees the same output.

    Just like in the English language:

    The concept of context sensitive meaning is not specific to C++, you can even see it in the English language.

    • I am going to screen a movie (Means showing the movie)
    • The screen on the TV is broken (Means a part of the TV)

    Other meanings in other programming languages:

    Depending on the programming language there can be a different meaning, but the first thing most people think of when you say static is a class variable/function vs a member variable/function.

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

Sidebar

Related Questions

There have been a few questions asked here about why you can't define static
I have few questions about ssis transction isolation levels. consider a scenario:I have an
I have few questions about using lock to protect my shared data structure. I
I have a few questions about Xcode and interaction with GCC 4.2.1: It doesn't
I have a few questions about this after reading the iPhone documentation on it:
I have a few questions about cross-page posting in ASP.NET: What is cross-page posting
I have a few questions about Core Location. 1) Should the user refuse permission
I have a few questions about the eq() property of jQuery and how to
So I have a few questions about implementing the Android action bar. Up to
I just have a few questions about programmatically adding controls in aspx . After

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.