473,473 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

access specifiers for friend functions

Hi Everyone,

The following code works fine,

class A
{
private: friend void sample(A& obj)
{
printf("in friend...\n");
printf("%d",obj.i);
}
public: A()
{
this->i = 5;
}
private: int i;
};

int main()
{
A obj;
sample(obj);
return(0);
}

I'm wondering what could be the significance of the private access
specifier to the friend function, what difference does it make?

Thanks in advance!!!
Jan 8 '08 #1
3 2454
On Jan 7, 10:09 pm, Rahul <sam_...@yahoo.co.inwrote:
Hi Everyone,

The following code works fine,

class A
{
private: friend void sample(A& obj)
{
printf("in friend...\n");
printf("%d",obj.i);
}
public: A()
{
this->i = 5;
}
private: int i;

};

int main()
{
A obj;
sample(obj);
return(0);
}

I'm wondering what could be the significance of the private access
specifier to the friend function, what difference does it make?

Thanks in advance!!!
Friend functions are external functions which don't belong to the
scope of the class and also access specifiers don't apply.

HTH.
Jan 8 '08 #2
In article
<f7**********************************@e25g2000prg. googlegroups.com>,
Rahul <sa*****@yahoo.co.inwrote:
Hi Everyone,

The following code works fine,

class A
{
private: friend void sample(A& obj)
{
printf("in friend...\n");
printf("%d",obj.i);
}
public: A()
{
this->i = 5;
}
private: int i;
};

int main()
{
A obj;
sample(obj);
return(0);
}

I'm wondering what could be the significance of the private access
specifier to the friend function, what difference does it make?
None. The private access specifier is not directly tied to the friend
function in any way. It seems as though you are putting the access
specifier at the beginning of each declaration, but that is unnecessary.

Normally I format my classes like this:

class Name
{
public:
// public functions
protected:
// protected functions
private:
// private functions
// private data
//friend functions
};

So for your class above, I would write:

class A
{
public:
A();
private:
int i;
friend void sample( A& obj );
};

A::A(): i( 5 ) { }

void sample( A& obj )
{
cout << "in friend...\n";
cout << obj.i;
}
Jan 8 '08 #3
On Jan 7, 10:09 pm, Rahul <sam_...@yahoo.co.inwrote:
Hi Everyone,

The following code works fine,

class A
{
private: friend void sample(A& obj)
{
printf("in friend...\n");
printf("%d",obj.i);
}
public: A()
{
this->i = 5;
}
private: int i;

};

int main()
{
A obj;
sample(obj);
return(0);
}

I'm wondering what could be the significance of the private access
specifier to the friend function, what difference does it make?

Thanks in advance!!!
friend functions, like sample(A& obj) are more like gang members. They
respect no rules, access specifiers don't apply.
Specifically, someone could change sample(A& obj) and modify the
internal parts of your objects.

Some friends make sense (like usefull operators that need access to
your private parts).
These don't violate your encapsulation since the instance of your
class is passed as a reference_to_const.
(note: const A& below)

#include <iostream>
#include <ostream>

class A
{
int i;
public:
A() : i(5) { }
// friend op
friend std::ostream&
operator<<(std::ostream&, const A&);
};

std::ostream&
operator<<(std::ostream& os, const A& a)
{
os << "i = " << a.i;
return os << std::endl;
}

int main()
{
A obj;
A another;
std::cout << obj << another << std::endl;
}

/*
i = 5
i = 5
*/
Jan 8 '08 #4

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

Similar topics

6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
10
by: Steven T. Hatton | last post by:
I just read something interesting in one of the PDFs located here: http://www.cs.wustl.edu/~schmidt/C++/ Sorry, I don't recall which file it was, and I'm too lazy to dig it up again ;) It says...
2
by: DotNet | last post by:
hi, which are the different access specifiers in vb.net other than public ,private .
6
by: vipul DotNet | last post by:
hi, which all are the access specifiers in vb.net thank you vipul
8
by: Tapeesh | last post by:
I have a following piece of code. The code was compiled using g++ class A { public : virtual void fn() = 0; }; class B: virtual private A {
2
by: Ahmed | last post by:
Ok i understand the fact that once events are raised they are executed on another thread and have no access to member functions or variables because they are static functions. But i am having...
1
by: curious2007 | last post by:
Hello everybody, When I try to declare nontemplate friend functions I get the following warning: friend decleration......declares a non-template function (if this is not what you intended,...
0
by: Pooria | last post by:
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char char2; }; class Test2{
3
by: david sherman | last post by:
Does software converting excel to access with financial functions exist? I need to take several fixed income portfolio run manually from excel and create a database. The excel relies heavily on...
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
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
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...
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 project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.