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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:42:49+00:00 2026-06-10T18:42:49+00:00

This is a how C++ works question. I’ve been looking into the friend modifier

  • 0

This is a how C++ works question.

I’ve been looking into the friend modifier and found an example of a static friend method here.

But now I’m having trouble understanding why certain things were done to make it work.

I’m also curious of what practical application this could be used for? When would you want to use static friend? Should this be avoided?

Here’s the code with comments added to point out parts I’m confused about.

#include <iostream>

class A; //1. why declare class A here and define it below?

class B
{
public:
    B();
    ~B();

    static void SetAiPrivate(int value); //Makes SetAiPrivate static
    static A *pa; //static instance of class A for class B's static 
                  //methods to use
};

class A
{
friend void B::SetAiPrivate(int); //Gives Class B's SetAiPrivate method access
                                  //to A's private variables

public:
    A(){iPrivate = 0;}
    ~A(){}
    void PrintData(){ std::cout << "iPrivate = "<< iPrivate<<"\n";}

private:
    int iPrivate;
};


A *B::pa;//2. Why is this needed? 
         //   If commented out it causes an external linking error.

B::B()
{
    pa = new A;
}     
B::~B()
{
    delete pa;
}

void B::SetAiPrivate(int value)
{
    pa->iPrivate = value;
}

int main()
{
    B b; //3. Is this necessary? Doesn't C++ automatically initiate static
         //   member variables when a class is referenced

    B::SetAiPrivate(7);
    B::pa->PrintData();
    return 0;
}
  • 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-10T18:42:50+00:00Added an answer on June 10, 2026 at 6:42 pm

    Let’s look at the code first:

    class A; //1. why declare class A here and define it below?

    That’s a forward declaration. B has a member of type A*, so A must be declared beforehand.

    A *B::pa;//2. Why is this needed?

    Static data members are only declared inside the class definition. That’s the definition and it’s required by the standard to be present in a single translation unit.

    B b; //3. Is this necessary? Doesn’t C++ automatically initiate static
    // member variables when a class is referenced

    Not necessary. Unless of course the static methods in B depend on the run of the constructor. And if they do, it’s a bad design.

    Onto the friend question. Usually, having friends breaks encapsulation, be them only members or a whole class. This is no different. It just tells the compiler that A::iPrivate is directly accessible from B::SetAiPrivate.

    This looks wrong to me, as you’d probably want to be able to set A‘s member directly from A, not from B.

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

Sidebar

Related Questions

This works but seems incredibly clumsy. The method in question is streetsInObj, and obj
This question falls into the yes - this works, yes - this is ugly,
I have this old question but no answer works for me from online, the
NOTE: This is an old question and the answers here no longer works (since
This is just a simple question. Either way works. I prefer my first example,
I fallowed @Stefan Monov tutorial in this question . And everything works, but I
This is probably a nub question, but I don't understand why this works: <script
Simple question really. This works in all but IE6: <html> <body> </body> <select id=test
This question started out here . But changed significantly as I learned more about
This is my first question here but I'm sure there are many more to

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.