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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:04:15+00:00 2026-05-15T00:04:15+00:00

I have a class with a protected method Zig::punt() and I only want it

  • 0

I have a class with a protected method Zig::punt() and I only want it to be accessible to the class “Avocado”. In C++, you’ll normally do this using the “friend Avocado” specifier, but this will cause all of the other variables to become accessible to “Avocado” class; I don’t want this because this breaks encapsulation.

Is what I want impossible, or does there already exist an obscure trick out there that I can use to achieve what I want? Or possibly alternative class design patterns that’ll achieve the same thing?

Thanks in advance for any ideas!

  • 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-05-15T00:04:15+00:00Added an answer on May 15, 2026 at 12:04 am

    Here’s an ugly yet working trick:

    class AvocadoFriender {
    protected:
      virtual void punt() = 0;
      friend class Avocado; 
    }
    
    class Zig : public AvocadoFriender {
      ...
    protected:
      void punt();
    }
    

    Basically you add a mixin class that exposes only the part of the interface that you want to Avocado. We take advantage of the fact that by inheriting a class that is befriended to Avocado you don’t expose anything more except what was exposed originally.

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

Sidebar

Related Questions

I have this little OO problem.. abstract class MyAbstractBaseClass { MyHelperClassBase myHelperBaseClass; protected method
I have this code in base class protected virtual bool HasAnyStuff<TObject>(TObject obj) where TObject:class
I have this class: public class Source extends Node { protected DistributionSampler delay ;
Is it possible to call a protected method via reflection. I am using this:
I have two classes. Class A has protected method m() , a is an
i have a class say A that has a protected method say methodA(), then
Say I have the following hierarchy: public class MyClass { protected virtual void Method()
Hi I have a class A with as abstract method protected abstract List<Contributor> GetContributors(List<SyndicationPerson>
I have a class that looks something like this: class Foo { protected $_arr
I have this class/interface definitions in C# public class FooBase { ... protected bool

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.