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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:13:39+00:00 2026-06-09T07:13:39+00:00

I have a question with friend function in C++. I understand that if a

  • 0

I have a question with friend function in C++. I understand that if a function is defined as a friend function of a class, it can access any member variables or function regardless whether it is private, protected or public. Recently I am using doxygen to create document reference, I find another advantage of friend function: its relation with the class can be easily illustrated as friend function will be listed after the member function in the HTML page. However, if the function is not defined as friend, it will be regarded as a global function, and with doxygen it will not be listed with the class documentation. Then I plan to make all the global functions that have relationship with a specific class as its friend functions. I was wondering whether this is a good practice.

The following codes show one function can be chosen as either as a friend function or a global function.

#include <iostream>
#include <map>
#include <set>
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <numeric>
#include <string>
using namespace std;

class ABCD
{
public: 
    int a;
    int b;
    friend void friend_fun(const ABCD &obj);
};

void fun(const ABCD &obj)
{
    std::cout<<obj.a<<endl;
    std::cout<<obj.b<<endl;
};

void friend_fun(const ABCD &obj)
{
    std::cout<<obj.a<<endl;
    std::cout<<obj.b<<endl;
};


int main () 
{
     ABCD obj;
     obj.a = 20;
     obj.b = 30;
     fun(obj);
     friend_fun(obj);

    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-09T07:13:42+00:00Added an answer on June 9, 2026 at 7:13 am

    Doxygen allows you to create and refer to groups of functions and other global things. That would make a lot more sense than breaking useful language-level protection just to exploit a quirk of Doxygen’s behaviour.

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

Sidebar

Related Questions

I have question regarding the SQLAlchemy. How can I add into my mapped class
This is a pretty basic C++ design question: I have a class that contains
I understand that friend is not inherited. I have classes Parent Person Child .
I have Question and QuestionTypes. In Question table there is a foreign key that
I have question i.e how can i select a previous div CODE:- if($i >
I have question regarding the use of function parameters. In the past I have
I have question concerning a function I created. I would like to show the
Apologies for the rather simple question, I just can't seem to find ANY good
I have a random question about const correctness. Lets say i have a class
Is their a way to use a non-member non-friend function on an object using

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.