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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:37:38+00:00 2026-06-18T15:37:38+00:00

Normally in C++ when class A declares friendship with class B, class B has

  • 0

Normally in C++ when class A declares friendship with class B, class B has full access to class A private members. What I need to do is to allow class B to access only one private member of class A and nothing else. Is there any way for it, maybe something new in C++11?

  • 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-18T15:37:38+00:00Added an answer on June 18, 2026 at 3:37 pm

    Not that I’m aware of. If it’s really important to only allow access to a single member, you could wrap the data in another class (C) that has entirely private members, make that class a friend of B and provide a public accessor for the C object in A.

    Something like this:

    template<class T> class MatesWithB
    {
         friend class B;
    
      public:
         MatesWithB( T & val ) : data(val) {}
    
      private:
         T& data;
         operator T&()             { return data; }
         operator const T&() const { return data; }
    };
    
    class A
    {
          // ...
          MatesWithB<int> GetPrivateThingy() { return MatesWithB(thingy); }
    
       private:
          int thingy;            // Shared with class B
    };
    

    Something like that. I haven’t run this through a compiler to check.

    But I just wonder… When you find yourself needing to do this, isn’t something in your design fundamentally flawed?

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

Sidebar

Related Questions

A class is composed normally of member variables & methods. When we create instance
Normally when I have a private field inside a class or a struct, I
Inside of a static member function I need to get the type. class MyClass
I have one class called dog and another class called cat. Dog has an
I coded an entity helper class and normally I use someting like that private
I have a class with const members, and one constructor which calls another constructor
I have a dynamic class, which has an array declared normally, before runtime. Usually,
normally I implement my Runnables as follows (directly implemented inner class): Runnable updateRunnable =
Normally I would go: bgwExportGrid.RunWorkerCompleted += ReportManager.RunWorkerCompleted; The ReportManager class is a static class
Use case: class MyTask(Task): queue = 'default_queue' def run(self): # do work Normally I

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.