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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:06:49+00:00 2026-06-08T18:06:49+00:00

I have a namespace foo which contains an integer bar , declared so… foo.h:

  • 0

I have a namespace foo which contains an integer bar, declared so…

foo.h:

namespace foo {
    int bar;
}

Now if I include foo.h in only one file, this works just fine. But a problem arises when I include foo.h from two or more files: I get a linker error. I figured out that if I declare bar as static, I can include foo.h in more than one file. This seems strange to me, because I wasn’t aware that one could declare a static variable inside of a namespace. (what does that even mean?)

Why does this work? And more importantly, why doesn’t it work without static? What does static mean when used in a namespace?

  • 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-06-08T18:06:50+00:00Added an answer on June 8, 2026 at 6:06 pm

    There are multiple meanings for static in different contexts. In this particular context it means that the variable has internal linkage, and thus each translation unit that includes that header will have it’s own copy of the variable.

    Note that while this will silent the linker error, it will do so maintaining a separate foo::bar variable for each one of the object files generated (changes will not be visible across different object files).

    If you want a single variable, you should declare it as extern in the header and provide a single definition in one translation unit.

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

Sidebar

Related Questions

I have the following code I'm refactoring: namespace Foo { namespace Bar { ...classes...
I have two namespaces: 1) Foo.Bar 2) Another.Foo.Bar From a class in namespace 2,
I have a select defined in mapper.xml file: <mapper namespace=foo> <select id=selectWithRoles parameterType=String resultMap=personWithRoles>
I have a header file in which I wish to use a namespace alias
If a function is declared as such: namespace foo { void bar(); } Most
If I have a variable, $bar , which is equal to string foo and
I have a function in my namespace, ns::foo , whose job is to dispatch
I have a module Namespace/Search, which was installed when I got here. I took
I have the following code which i m trying to compile: #include <boost/filesystem/convenience.hpp> #include
If I write the following code: #include <iostream> using namespace std; int main() {

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.