access specifiers for friend functions

R

Rahul

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!!!
 
P

Prashanth

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.
 
D

Daniel T.

Rahul said:
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;
}
 
S

Salt_Peter

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::eek:stream&
operator<<(std::eek:stream&, const A&);
};

std::eek:stream&
operator<<(std::eek:stream& 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
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top