Topic: Scope of functions defined in friend declarations (?)
Author: speros@csri.toronto.edu (Speros Armyros)
Date: 8 Nov 91 03:00:44 GMT Raw View
Hello everyone,
I've got a question for all of you regarding the scope of friend functions.
ARM says on page 187 (last paragraph) that "Like a member function, a
friend function defined within a class is in teh lexical scope of that
class;".
I believe this rule is incomplete. It only pertains to friend functions
that are not member functions of another class.
Consider the case in the following source segment:
extern "C" int printf( const char * ...);
int ::i = 5 ;
class OUT1{
public:
static i;
void f();
};
class OUT2{
public:
static i;
friend void OUT1::f() {
printf("i = %d\n", i); // <-- Which i should be printed ?
}
};
// Definition of Static members
int OUT1::i = 10;
int OUT2::i = 20;
void main() {
OUT1 obj;
obj.f();
}
Class OUT2 defines the member function OUT1::f() in a friend function
declaration. What would be the order that a compiler will do a look-up
for the name i ? Would that be :
void OUT1::f() (scope of member function)
class OUT1 (scope of class OUT2 )
global scope
Would the scope of class OUT2 ever be considered. In other words if
none of ::i, OUT1::i is defined, would the compiler ever resolve i to
OUT2::i ?
By the way CFRONT gives a link error in this case (unresolved OUT1::f)!!!!
Thanks a lot in advance.
Speros Armyros