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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:11:38+00:00 2026-06-14T01:11:38+00:00

I have multiple classes that need to share a single instance of another class.

  • 0

I have multiple classes that need to share a single instance of another class. Publicly it should be unknown that this class exists. Is it appropriate to do something like the following? (Was tested as written)

#include <iostream>

class hideme
{
    private:
        int a;

    public:
        void set(int b) { a = b; }
        void add(int b) { a += b; }
        int get() { return a; }

        hideme() : a(0) { }
};


class HiddenWrapper
{
    protected:
        static hideme A;

};

hideme HiddenWrapper::A;

class addOne : public HiddenWrapper
{
    public:
        void add() { A.add(1); }
        int get() { return A.get(); }
};

class addTwo : public HiddenWrapper
{
    public:
        void add() { A.add(2); }
        int get() { return A.get(); }
};


int main()
{
    addOne a;
    addTwo b;

    std::cout << "Initialized: " << a.get() << std::endl;

    a.add();
    std::cout << "Added one: " << a.get() << std::endl;

    b.add();
    std::cout << "Added two: " << b.get() << std::endl;

    return 0;
}

For what it’s worth, hideme is part of a library I’m attempting to design a facade around, and the other classes have members from the library that interact with the static hideme.

Additionally, if the header file written for HiddenWrapper has no corresponding source file, is that the best place to define its static member? With an include guard.

Is there any other method to solve this problem? As far as I could imagine (not terribly far) I could only solve it otherwise with friendship, which I am wary of.

  • 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-14T01:11:39+00:00Added an answer on June 14, 2026 at 1:11 am

    You can prevent access to a class by not making it accessible outside the translation unit that uses it.

    // public_header.h
    
    class A {
        void bar();
    };
    class B {
        void foo();
    }
    
    // private_implementation.cpp
    #include "public_header.h"
    
    namespace {
      class hidden { void baz() {} };
    
      hidden h;
    }
    
    void A::bar() {
        h.baz();
    }
    void B::foo() {
        h.baz();
    }
    

    This class will be usable only by A::bar and B::foo. The type hidden and the variable h still technically have external linkage, but no other translation unit can say their names.

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

Sidebar

Related Questions

I have multiple classes that I need to map into 1 class: This is
Is it possible to have multiple classes that inherit/extends same class in Google AppEngine
The situation: I have multiple classes that should each hold a variable with a
In Django, when you have a parent class and multiple child classes that inherit
I have got a single file in which I need specify classes in multiple
I have multiple classes and threads that need to write to a Java Swing
I have a set of variables that i need for multiple other classes. I
I have multiple classes (B, C and D) that are subclasses of class A.
I have multiple classes that inherit from one base class, and each class is
I have multiple projects that need to share resource files (.resx) Suggestions have been

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.