panJames
asked on
Friend classes and methods
Hello Experts!
Please have a look at the code below:
Please have a look at the code below:
#include <iostream>
using namespace std;
class F;
class X {
friend class F { };//error, it makes sense, friend declaration is not supposed to declare class
};
class A {
void g();
};
void z() {
class B {//we define class within method... weird but legal
friend void f() { }; //error, it makes sense, friend declaration is not supposed to declare method
};
}
class C {
friend void A::g() { } //error, it makes sense, friend declarion is not supposed to declare method
friend void h() { }//now I am confused! It is legal! Why? What logic is behind it? How does compiler understand it?
};
int main(){
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi panJames,
farzanj is right, removing the { } solves the friend class F in class X and the friend void A::g() in class C.
Somthing like friend void f(); doesn't make much sense since this declares the function f from the same class as friend which is meaningless. You can i.e. declare a global function as friend:
ZOPPO
farzanj is right, removing the { } solves the friend class F in class X and the friend void A::g() in class C.
Somthing like friend void f(); doesn't make much sense since this declares the function f from the same class as friend which is meaningless. You can i.e. declare a global function as friend:
void f(); // prototype
void z() {
class B {//we define class within method... weird but legal
friend void ::f(); //ok
};
}
void f() { /*do something*/ }
Hope that helps,ZOPPO
Oh and please note this is about definition - for example
void h() {}
void z() {
class B {
friend void h(); // works
friend void f() { }; // does not work
};
}
void h() {}
void z() {
class B {
friend void h(); // works
friend void f() { }; // does not work
};
}
ASKER
@ambience
Still confused...
To me C looks like a local class because I can see it's declaration. Why is it not local?
Another thing.
friend void h() { }
for me it looks like declaration of method 'h', method that does nothing so...
1. does compiler assume that somewhere in the code there is method:
void h() { }
?
2. or
class C {
friend void h() { cout<<"C++ is weird";}
};
does it mean that we can do such a thing:
class C {
int x, y;
friend void h() { cout<<"C++ is weird";}
};
h();
**** result: ****
C++ is weird
class C {
friend void h() { }//now I am confused! It is legal! Why? What logic is behind it? How does compiler understand it?
};
C is a non-local class therefore it works.
Still confused...
To me C looks like a local class because I can see it's declaration. Why is it not local?
Another thing.
friend void h() { }
for me it looks like declaration of method 'h', method that does nothing so...
1. does compiler assume that somewhere in the code there is method:
void h() { }
?
2. or
class C {
friend void h() { cout<<"C++ is weird";}
};
does it mean that we can do such a thing:
class C {
int x, y;
friend void h() { cout<<"C++ is weird";}
};
h();
**** result: ****
C++ is weird
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> a local class is a class which is declared within another class or a function
Half true - a local class is one declared inside a function. A class inside another class is nested but not local. You can certainly do
This is nested, not local.
>> The friend keyword is meaningless here since a method always has access to private members of their class.
>> for me it looks like declaration of method 'h', method that does nothing so...
NO - its not meaningless and its NOT Method, its a function (that is not gobal). The following does not compile, because h is not a method - take out the friend and it compiles.
>> does it mean that we can do such a thing:
No, since its not in global scope - but you can use it like in the example above.
>>.C++ is weird
When you dont know what you are doing its always weird.
Half true - a local class is one declared inside a function. A class inside another class is nested but not local. You can certainly do
class A { public: class B { }; };
A::B b;
This is nested, not local.
>> The friend keyword is meaningless here since a method always has access to private members of their class.
>> for me it looks like declaration of method 'h', method that does nothing so...
NO - its not meaningless and its NOT Method, its a function (that is not gobal). The following does not compile, because h is not a method - take out the friend and it compiles.
class C
{
friend void h() {
cout << "Not a method so this is not defined " << this << std::endl; << ERROR!
}
public:
void method()
{
h();
}
};
>> does it mean that we can do such a thing:
No, since its not in global scope - but you can use it like in the example above.
>>.C++ is weird
When you dont know what you are doing its always weird.
ok, sorry, ambience is right. Only within a function it's a local class. And the 'h()' is a function which is not a method. Honestly I have to admit I didn't know that's even possible :(
friend class F { };
Should be
friend class F;
Same is the problem with the friend member function. You are only supposed to give a prototype preceded by word friend not the definition so you cannot have {}
With non member function (where you said is confusing), you are giving a definition as well and it will be a non member function but being friend will be able to access the protected and private data of the class.