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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:09:10+00:00 2026-06-09T04:09:10+00:00

I was wondering if it was possible to restrict access to certain functions or

  • 0

I was wondering if it was possible to restrict access to certain functions or objects declared in a namespace exclusively to other classes in the same file. For instance,

//myheader.h

namespace stuff
{
  int repair(firstObj obj);
  int doSomethingElse();
  privateObj obj;
}

In this case, I want the function doSomethingElse() and the object obj to be accessible to classes declared in this file only.

Is there some keyword I could use to restrict access?

  • 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-09T04:09:12+00:00Added an answer on June 9, 2026 at 4:09 am

    use an unnamed namespace:

    namespace stuff
    {
    
      int repair(firstObj obj);
    
      namespace 
      {
        int doSomethingElse();
        privateObj obj;
      }
    
    }
    

    From here [emphasis mine]:

    Unnamed namespaces are a superior replacement for the static declaration of variables.
    They allow variables and functions to be visible within an entire translation unit, yet not visible externally. Although entities in
    an unnamed namespace might have external linkage, they are effectively
    qualified by a name unique to their translation unit and therefore can
    never be seen from any other translation unit.

    See also this related question: Unnamed/anonymous namespaces vs. static functions


    Edit: Just noted that it’s a header file we’re talking about:

    In this case, I want the function doSomethingElse() and the object obj to only be accessible to classes declared in this file.

    Then you shouldn’t declare these methods and objects in a public header file in the first place, but rather in the specific implementation file: If other shall not use them, other should not even know they’d exist.

    And then use an unnamed namespace to effectively restrict the reachability (e.g. if someone would accidentally provide another declaration using the same identifier).

    If you leave it in the header file, this could happen: anonymous namespaces and the one definition rule (if the header is included in more than one translation unit):

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

Sidebar

Related Questions

I was wondering if its possible to directly cast objects within a foreach loop.
I've been wondering: is it possible to shield a directory/file on a server from
i'm wondering is that possible to indicate the mask for file type by default
I was wondering if it possible to access iPhone User Music, Videos, Photos and
I was wondering if is possible to find the content in an XML file
I was wondering how (if possible) would you access the return value of an
I am wondering is it possible to detect when binding is done? For instance
Just wondering if its possible to exclude large files or certian file types from
I just embedded Groovy in my application and wondering if it's possible to restrict
Wondering if its possible to concatenate keyPaths to one attribute in mapping objects. Looking

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.