| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
28 Jan 2008 04:57:36 AM |
| Object: |
Friend function |
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?
.
|
|
| User: "Michael DOUBEZ" |
|
| Title: Re: Friend function |
28 Jan 2008 05:48:12 AM |
|
|
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
.
|
|
|
| User: "" |
|
| Title: Re: Friend function |
28 Jan 2008 05:47:11 AM |
|
|
I know what you mean but its not what I meant. If you check
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/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.
.
|
|
|
| User: "Michael DOUBEZ" |
|
| Title: Re: Friend function |
28 Jan 2008 08:13:32 AM |
|
|
a écrit :
I know what you mean but its not what I meant. If you check
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/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
.
|
|
|
|
|
|
| User: "Jerry Coffin" |
|
| Title: Re: Friend function |
29 Jan 2008 12:11:59 AM |
|
|
In article <101409dc-b651-44fd-8c1c-
624779cb6ded@j20g2000hsi.googlegroups.com>,
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;
}
};
--
Later,
Jerry.
The universe is a figment of its own imagination.
.
|
|
|
|

|
Related Articles |
|
|