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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:26:01+00:00 2026-05-23T20:26:01+00:00

Possible Duplicate: When should you use 'friend' in C++? I have come to a

  • 0

Possible Duplicate:
When should you use 'friend' in C++?

I have come to a stumbling block because of lack of documentation on friend classes. Most books just explain it briefly, e.g an excerpt from C++: the Complete Reference:

Friend Classes are seldom used. They are supported to allow certain special case situations to be handled.

And frankly, I have never seen a friend class in any good code made by an experienced C++ programmer. So , here is my list of problems.

  1. Do Inherited Classes have the same friends as there base classes? e.g, if I declare class foo as a friend of class base, will class der (derived from base) also have foo as a friend?

  2. What are the special case situations when a friend class should be used?

  3. I am making a winapi wrapper in which I want to make class WinHandle a friend of class Widget (to access some protected members). Is it recommended? Or should I just access them using the traditional Get/Set functions?

  • 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-23T20:26:02+00:00Added an answer on May 23, 2026 at 8:26 pm

    Friend is used for granting selective access, just like the protected access specifier. It’s also hard to come up with proper use case where use of protected is really useful.

    In general, friend classes are useful in designs where there is intentional strong coupling: you need to have a special relationship between two classes. More specifically, one class needs access to another classes’s internals and you don’t want to grant access to everyone by using the public access specifier.

    The rule of thumb: If public is too weak and private is too strong, you need some form of selected access: either protected or friend (the package access specifier in Java serves the same kind of role).

    Example design

    For instance, I once wrote a simple stopwatch class where I wanted to have the native stopwatch resolution to be hidden, yet to let the user query the elapsed time with a single method and the units to be specified as some sort of variable (to be selected by user preferences, say). Rather than, have say elapsedTimeInSeconds(), elapsedTimeInMinutes(), etc. methods, I wanted to have something like elapsedTime(Unit::seconds). To achive both of these goals, I can’t make the native resolution public nor private, so I came up with the following design.

    Implementation overview

    class StopWatch;
    
    // Enumeration-style class.  Copy constructor and assignment operator lets
    // client grab copies of the prototype instances returned by static methods.
    class Unit
    {
    friend class StopWatch;
        double myFactor;
        Unit ( double factor ) : myFactor(factor) {}
        static const Unit native () { return Unit(1.0); }
    public:
            // native resolution happens to be 1 millisecond for this implementation.
        static const Unit millisecond () { return native(); }
    
            // compute everything else mostly independently of the native resolution.
        static const Unit second () { return Unit(1000.0 / millisecond().myFactor); }
        static const Unit minute () { return Unit(60.0 / second().myFactor); }
    };
    
    class StopWatch
    {
        NativeTimeType myStart;
        // compute delta using `NativeNow()` and cast to
        // double representing multiple of native units.
        double elapsed () const;
    public:
        StopWatch () : myStart(NativeNow()) {}
        void reset () { myStart = NativeNow(); }
        double elapsed ( const Unit& unit ) const { return elapsed()*unit.myFactor; }
    };
    

    As you can see, this design achieves both goals:

    1. native resolution is never exposed
    2. desired time unit can be stored, etc.

    Discussion

    I really like this design because the original implementation stored the multiple of native time units and performed a division to compute the elapsed time. After someone complained the division was too slow, I changed the Unit class to cache the dividend, making the elapsed() method (a little) faster.

    In general, you should strive for strong cohesion and weak coupling. This is why friend is so little used, it is recommended to reduce coupling between classes. However, there are situations where strong coupling gives better encapsulation. In those cases, you probably need a friend.

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

Sidebar

Related Questions

Possible Duplicate: When should I use double or single quotes in JavaScript? Do ""
Possible Duplicate: When should I use stdClass and when should I use an array
Possible Duplicate: In C#, should I use string.Empty or String.Empty or “” ? I
Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer
Possible Duplicate: Handlers in Android I am working on Android projects. I need to
Possible Duplicate: PHP 5.3 changelog? I am new to PHP, and I am confused
Possible Duplicate: AuthComponent: Difference between allowedActions and allow()? What is the difference between using
Possible Duplicate: Differences in string compare methods in C# Is there any difference between
Possible Duplicate: python 2.6 or python 3.1? Hi, I'm new to the python world
Possible Duplicate: Interface vs Abstract Class (general OO) Hi guys, I decided to dig

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.