473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Philosophical reason behind the concept of friend function

Hi all,
I am a newbie in c++. I want to know what is the
philosophical reason behind the existence of friend functions. I
thought giving access to private data to a function which is not a
member of the class is a violation of encapsulation.

Thanks,
Madhav.

Mar 13 '06 #1
4 2321
Madhav wrote:
Hi all,
I am a newbie in c++. I want to know what is the
philosophical reason behind the existence of friend functions. I
thought giving access to private data to a function which is not a
member of the class is a violation of encapsulation.

Thanks,
Madhav.


Unfortunately, we're not living in an ideal world. That means that
sometimes (like when building singleton holder or some generic
framework) it's OK to access one's private properties. And, yeah,
sometimes (hey, I said SOMETIMES, don't kill me!) it is even appropriate
to use goto :o)
Mar 13 '06 #2
GB
Madhav wrote:
Hi all,
I am a newbie in c++. I want to know what is the
philosophical reason behind the existence of friend functions. I
thought giving access to private data to a function which is not a
member of the class is a violation of encapsulation.

Thanks,
Madhav.


It is only giving access to *arbitrary* functions (e.g., by making data
public) that is a violation of encapsulation. Having the class identify
a *specific* function for access is not a violation of encapsulation,
since the class is giving that access. The function becomes part of the
encapsulation along with the member functions. That said, it should be
rare that non-members need such access.

Gregg
Mar 13 '06 #3
Madhav wrote:
Hi all,
I am a newbie in c++. I want to know what is the
philosophical reason behind the existence of friend functions. I
thought giving access to private data to a function which is not a
member of the class is a violation of encapsulation.

Thanks,
Madhav.


The philosophical reason: to extend encapsulation boundary over that
defined by the class.

Ask yourself what is being encapsulated from whom :)

Regards,
Ben
Mar 13 '06 #4
In article <1142221918.688510.290930
@i39g2000cwa.googlegroups.com>, ma***********@gmail.com
says...
Hi all,
I am a newbie in c++. I want to know what is the
philosophical reason behind the existence of friend functions. I
thought giving access to private data to a function which is not a
member of the class is a violation of encapsulation.


The reason is pretty simple: sometimes functions that are
part of the interface to a class can't actually be member
functions. When this happens, the non-member function is
normally made a friend of the class.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
8
by: Steve Jorgensen | last post by:
Hi folks, I'm posting this message because it's an issue I come up against relatively often, but I can't find any writings on the subject, and I haven't been able to figure out even what key...
6
by: Beat | last post by:
Is it possible to reference the same codebehind from 2 different pages? It would be an elegant solution, where we do need different settings at the PAGE-Level for 2 user-Groups but the rest of...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
9
by: crescent_au | last post by:
Hi everybody, I want to create dynamic breadcrumbs for a site I am developing. I just want to get some views about the algorithm of breadcrumbs. The way I look at it is the concept of parent/...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
9
by: p | last post by:
I know the word friend is not supported in c# and its not a very good thing to but i would be thankful if anybody can explain in simple words how can i implement it!
6
by: Juha Nieminen | last post by:
Whenever one sees example C++ code basically anywhere, be it in a book, in a tutorial in the internet, in an online forum or whatever, I would estimate that at least in 99% of cases one sees the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.