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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:40:13+00:00 2026-05-18T06:40:13+00:00

Yet another static question. I have read the following: What are static variables? file

  • 0

Yet another static question.
I have read the following:

  • What are static variables?
  • file scope and static floats
  • http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx

And I still fail to understand the following behavior:
I have one h file:

// StaticTest.h
#include <stdio.h>

static int counter = 0;

struct A {
    A () {
        counter++;
        printf("In A's ctor(%d)\n", counter);
    }
    ~A () {
        counter--;
        printf("In A's dtor(%d)\n", counter);
    }
};

static A a;

And two cpp files:

// StaticTest1.cpp
#include "StaticTest.h"

int main () {
 return 0;
}

And:

// StaticTest2.cpp
#include "StaticTest.h"

The output of the program is:

In A's ctor(1)
In A's ctor(2)
In A's dtor(1)
In A's dtor(0)

Now, A‘s constructor is called twice, since the h file is included twice, and since A‘s instance named a is declared static, it has internal linkage and the compiler is happy.
Since the counter is also declared static, it also has internal linkage, and I would expect that it’s value will not be shared in the two cpp files — but the program output implies the value is shared, since it counts up to 2.

any insights?

EDIT:
Any answers regarding what is considered a “good programming habit” in the context of declaring static variables in h vs. cpp files is also welcomed.

  • 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-18T06:40:14+00:00Added an answer on May 18, 2026 at 6:40 am

    If StaticTest.h is shared between difference source files then you will get undefined behaviour.

    If you define a class or inline functions in different translation units then their definitions must be the same (same sequence of tokens) and, crucially, any identifiers must refer to the same entity (unless a const object with internal linkage) as in the definition in another translation unit.

    You violate this because counter has internal linkage so in different translation units the identifier in the function definitions refers to a different object.

    Reference: C++03 3.2 [basic.def.odr] / 5.

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

Sidebar

Related Questions

Yet another question, go me!... Anyway, I have 2 classes with private constructors and
so i'm following the OpenGL superbible 5 and have encountered yet another issue: I'm
I have yet another issue which the answer is eluding me. I wish to
After getting a helpful answer here , I have run into yet another problem:
I guess this question will sound familiar, but I am yet another programmer baffled
I'm relatively new to concurrency in Java (still have yet to read JCIP, but
EDIT: Yet another followup at https://stackoverflow.com/questions/1799742/shouldnt-netusermodalsget-tell-me-what-domain-a-machine-is-part-of-and-where Thanks to Gonzalo's help, I'm able to use
Yet another one of my P/Invoke questions! I have this C function: int _ei_x_new(ei_x_buff*
I'm working on yet another framework for PHP, and have run into a small
Here's yet another VC9 vs. GCC 4.2 compile error problem. The following code compiles

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.