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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:24:48+00:00 2026-06-10T14:24:48+00:00

I have been reading through other questions on here and there is something that

  • 0

I have been reading through other questions on here and there is something that has me confused and hopefully it can be explained. I am sure there it is a simple thing but it is alluding me.

So in C++ we have private variables that are only viewable within the class:

class MyClass
{
    private:
        int i;
};

But we can also have unnamed namespaces:

namespace
{
    int i;
}

Both appear to be private to the class but in the 2nd case you cannot see they exist from the header file. From reading other questions it seems that functions are different as you can’t pass class objects to them? But I am not sure what the difference is here for variables.

Is there a disadvantage to the 2nd way that means you should still use private variables?

  • 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-10T14:24:49+00:00Added an answer on June 10, 2026 at 2:24 pm

    They aren’t the same.

    Integer i in the anonymous namespace will be shared by all instances of MyClass.

    The private integer i in MyClass will be unique for each instantiation of the class.

    The equivalent using private would be to make i static:

    //.h
    class MyClass
    {
        private:
            static int i;
    };
    

    And instantiate the one single shared i like this:

    //.cpp
    int MyClass::i = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading through various questions on here, as I am learning VHDL
I've been reading through a nice tutorial and have a quick question concerning something
I have been reading through questions * and answers about how to stop an
I've been browsing through stackoverflow reading other questions about MSTest and DeploymentItem; I've followed
I have been reading through the backlog of answered questions on SO regarding How
I have been reading through this wonderful website regarding the recommended Python IDEs and
I have been reading through the documentation on the JavaScriptMVC framework and it looks
I have been reading up on the changes that .NET4.5 will bring, and on
I have been reading up on File's Owner here on Stack Overflow and a
I've been reading through the other posts on this topic tried what was suggested

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.