You are here:

C++/friend function


Can we make the main function a friend function?


There appears to be no restriction on doing so listed in the ISO C++ standard.
It also worked on two compilers I had to hand (Microsoft Visual C++ 2008 and Gnu g++ 4.2.4):

   class C
   friend int main();

       int v;
   int main()
       C c;
       c.v = 1;

However the question should not be:

   "Can we make the main function a friend function?"

so much as:

   "Should we make the main function a friend function?"

Friendship should be used sparingly and only between classes and functions that are closely related and in cases where not using friend declaration(s) would in fact expose more of the internal workings of a class to more parts of the code base than is strictly required.

That is the alternative to declaring friends would be to provide more public access to certain parts of a class - meaning all other parts of the code could access these parts - when in fact only a small subset of related classes and/or functions need access to these parts.

As main is the entry point into a program it cannot be said to be closely related to any classes, therefore I see little need to make main a friend of any class. Doing so would be a code 'bad smell' - it would indicate that something is probably wrong with the design or maybe just the implementation. Of course there are probably some interesting and valid exceptions, but I have never come across one (which is why I had to double check - I have never considered doing such a thing).  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.