473,804 Members | 3,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2363
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.688 510.290930
@i39g2000cwa.go oglegroups.com> , ma***********@g mail.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
3630
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: virtual void do_bar() {} };
8
2009
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 words one would use to look for it. First, in broad philosophical terms, code actually -is- data. Code is the data that's fed into a compiler, interpreter, or microprocessor that tells it what to do. Code execution is, then, just a form another...
6
1362
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 the page is the same. Page A: ....Codebehind="pagea.aspx.vb" validateRequest="True" ... Page B:
171
7806
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) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little overkill. I use Dreamweaver to do my design and find it a bit of a hassle to have multiple files open...
9
4424
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/ child (main/ sub). I have the "Home" page, which has links to sub-pages such as "Introduction", "History", "Timetable", etc. Each page can have other sub-pages, such as "Timetable" can have "Semester1", "Semester2", etc. This is how I'd initially...
15
2148
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 question when I was thinking about implementation of a class implementing some lazy data structure or cache. The C++ rules allow among other possibilities making all member functions non-const, or making all member functions const and all members...
2
4828
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 required: <%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %> Assuming that the ASPX page doesn't use a code-behind, I can access the properties, events etc. of the user control in the ASPX page in this way (assume that the ASPX page...
9
1676
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
2254
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 use of "using namespace std;" to get rid of that namespace. In fact, "using namespace ..." is very popular with all documentation and example code of most C++ libraries out there which use their own namespace. This raises the question why use...
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.