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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:56:27+00:00 2026-06-08T00:56:27+00:00

foo.h class Foo {}; namespace { class Bar {}; //… } foo.cpp Foo foo;

  • 0

foo.h

class Foo {};

namespace {
    class Bar {};
   //...
}

foo.cpp

Foo foo; // Ok

Bar bar; // Ok, but I want Error!

How to hide (insulate) class Bar in *.cpp file?

i.e. I do not want to import the class Bar {} from *.h file into *.cpp file

Perhaps there is some kind of encapsulation technology for classes?

  • 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-08T00:56:28+00:00Added an answer on June 8, 2026 at 12:56 am

    It’s not clear exactly what you’re asking for. If you only want Bar to be visible inside a particular .cpp file, that’s pretty easy:

    foo.h:

    // add include guard here.
    class Foo {};
    

    foo.cpp:

    #include "foo.h"
    
    namespace {
        class Bar {};
    }
    
    Foo foo;
    
    Bar bar;
    

    Note that this restricts all visibility of Bar to foo.cpp though. Nothing outside foo.cpp will realize that it even exists.

    There’s not really a way to make a class only visible inside a header though — the whole point of a header is that you include it in one or more .cpp files, and when you do that, whatever it contains becomes visible inside that .cpp file.

    If that’s what you think you want, chances are pretty good that you need to take a step back and tell us what you’re really trying to accomplish. There’s probably a way, but the way you’re trying to go is almost certainly wrong. The basic division is that headers are for things that will be visible in general; anything that’s private gets restricted to implementation (.cpp) files.

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

Sidebar

Related Questions

I have two namespaces: 1) Foo.Bar 2) Another.Foo.Bar From a class in namespace 2,
Given an assembly that contains namespace Foo{public class Bar;} How could I create an
My code: #include <iostream> using namespace std; class Foo { public: int bar; Foo()
Given: namespace A { class Foo; class Bar; } namespace B { class Foo;
Assuming a class called Bar in a namespace called foo , which syntax do
I have an object of class Foo: class Foo extends Bar { protected $a;
If I apply a custom attribute to a class, for example: [Foo] class Bar
//Using g++ and ubuntu. #include <vector> using namespace std; Define a class: class foo(){
Simplified version of the code: Foo.h: class Foo { private: class Bar { //
Example: // can't forward declare with class Foo::Bar // actual class class Foo {

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.