P
Peter
We all know what a friend function of a given class is: it's a
function which is not a member of the class, but has an access to its
non-public members. Usually we just declare a friend function inside a
class definition and define it elsewhere (in global scope or as a
member function of another class):
class foo
{
friend void friendly_function(); // declaration without
definition
};
void friendly_function() {some code} // definition in a global
scope
Defined this way, friendly_function() is visible anywhere from where
it's defined to the end of file (unless overridden in some local scope
by a variable with the same name).
However, it's perfectly legal to place the DEFINITION of a friend
function inside the definition of our class:
class foo
{
friend void friendly_function() {some code} // definition inside a
class definition
};
My questions are:
1. What is the scope of friendly_function() now?
2. What is the purpose of defining a friend function inside a
definition of a class which befriends it? It seems quite confusing and
illogical to me, my intuition tells me only a declaration should be
allowed there. After all, why would anyone define a function which is
NOT a member of a class inside that class? Since this is syntactically
correct, there must be a reason for it, so please give me an example
of situation where such a definition can be useful.
Here comes my doubt. I have two, almost identical, snippets of code:
#include <iostream>
using namespace std;
class foo
{
int x;
friend void friendly_function(){} //empty DEFINITION
};
int main()
{
friendly_function();
return 0;
}
which doesn't compile: g++ and Comeau give an error message about
"friendly_function" identifier not being visible in main(), Visual C++
seems to be more precise: " 'friendly_function': candidate function(s)
not
accessible could be the friend function at :
'friendly_function' [may be found via
argument-dependent lookup]".
Interestingly though, the following fragment is compiled by all three:
#include <iostream>
using namespace std;
class foo
{
int x;
friend void friendly_function(foo&){} // empty
DEFINITION, like before
};
int main()
{
foo obj;
friendly_function(obj);
return 0;
}
As you can see, the only difference is that previously
friendly_function() took no arguments and now an object of foo class
is passed to it by reference. Why does such a small change change the
scope of friendly_function()? Previously it was inaccessible inside
main() to all three compilers and now all three have access to it.
Why?
function which is not a member of the class, but has an access to its
non-public members. Usually we just declare a friend function inside a
class definition and define it elsewhere (in global scope or as a
member function of another class):
class foo
{
friend void friendly_function(); // declaration without
definition
};
void friendly_function() {some code} // definition in a global
scope
Defined this way, friendly_function() is visible anywhere from where
it's defined to the end of file (unless overridden in some local scope
by a variable with the same name).
However, it's perfectly legal to place the DEFINITION of a friend
function inside the definition of our class:
class foo
{
friend void friendly_function() {some code} // definition inside a
class definition
};
My questions are:
1. What is the scope of friendly_function() now?
2. What is the purpose of defining a friend function inside a
definition of a class which befriends it? It seems quite confusing and
illogical to me, my intuition tells me only a declaration should be
allowed there. After all, why would anyone define a function which is
NOT a member of a class inside that class? Since this is syntactically
correct, there must be a reason for it, so please give me an example
of situation where such a definition can be useful.
Here comes my doubt. I have two, almost identical, snippets of code:
#include <iostream>
using namespace std;
class foo
{
int x;
friend void friendly_function(){} //empty DEFINITION
};
int main()
{
friendly_function();
return 0;
}
which doesn't compile: g++ and Comeau give an error message about
"friendly_function" identifier not being visible in main(), Visual C++
seems to be more precise: " 'friendly_function': candidate function(s)
not
accessible could be the friend function at :
'friendly_function' [may be found via
argument-dependent lookup]".
Interestingly though, the following fragment is compiled by all three:
#include <iostream>
using namespace std;
class foo
{
int x;
friend void friendly_function(foo&){} // empty
DEFINITION, like before
};
int main()
{
foo obj;
friendly_function(obj);
return 0;
}
As you can see, the only difference is that previously
friendly_function() took no arguments and now an object of foo class
is passed to it by reference. Why does such a small change change the
scope of friendly_function()? Previously it was inaccessible inside
main() to all three compilers and now all three have access to it.
Why?