Friend function

M

michael.goossens

It has come to my understanding that it is possible to implement
friend functions in non-local classes. But what is the difference with
a normal function? Can't the function already access the private data
of the class ...?

class B{
int x;
friend int addOne(int y){ return x + y};
}

vs

class B{
int x;
int addOne(int y){ return x + y);
}

whats the difference?
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
It has come to my understanding that it is possible to implement
friend functions in non-local classes. But what is the difference with
a normal function? Can't the function already access the private data
of the class ...?

class B{
int x;
friend int addOne(int y){ return x + y};
}

vs

class B{
int x;
int addOne(int y){ return x + y);
}

whats the difference?


Friend function are granted to access protected members of a class but
they are functions not method.
The relevant example with your class is:

class B
{
//...
protected:
int x;
//...
friend int addOne(const class B& b,int y);
};

int addOne(const class B& b,int y)
{
return b.x + y;
}

int addTwo(const class B& b,int y)
{
//this fails because addTwo is not friend
// and cannot access b.x
return b.x + 2*y;
}

Usage is:
B b(42);
int c=addOne(b,36);

Michael
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
I know what you mean but its not what I meant. If you check
http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8l.doc/language/ref/cplr042.htm

You will clearly see that they make a difference between what you do
and actually implementing the function after the friend directive.

Sorry, I don't understand the problem. What do you mean by "actually
implementing the function after the friend directive" ?

How could you implement it *before* the friend declaration ? You need to
know the class in order to use it (except if you use only pointers and
references but then the friendship is useless).

The example you gave are meaningless:
class B
{
int x;
friend int addOne(int y){ return x + y;}
};
Here whether addOne() is a member function and friend directive doesn't
apply or it is supposed to be a free function and x is not in scope.

A valid friend is:
friend int addOne(const B& b,int y){ return b.x + y;}

Or has in the URL you gave:
class X;

class Y {
public:
void print(X& x);
};

class X {
int a, b;
friend void Y::print(X& x);
public:
X() : a(1), b(2) { }
};

void Y::print(X& x) {
cout << "a is " << x.a << endl;
cout << "b is " << x.b << endl;
}

Y::print(X&) is declared a friend of X and thus can access X protected
members X::a and X::b.

Michael
 
J

Jerry Coffin

(e-mail address removed)>, (e-mail address removed)
says...
It has come to my understanding that it is possible to implement
friend functions in non-local classes. But what is the difference with
a normal function? Can't the function already access the private data
of the class ...?

class B{
int x;
friend int addOne(int y){ return x + y};
}

This is equivalent to:

class B{
int x;
friend int addOne(int y);
};

int addOne(int y) { return x + y; }

including the fact that neither one will compile. Making a function a
friend grants it access to the class' private members, but regardless of
where you put the function body, it does NOT get a 'this' pointer, so
when it refers to a non-static class member, it still needs to specify
the object that owns the member to which it refers. For example:

class B {
int x, y, z;
friend ostream &operator<<(ostream &os, B const &b) {
os << b.x << "\t" << b.y << "\t", b.z;
}
};
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top