?
Solved

Friend classes and methods

Posted on 2013-01-23
8
Medium Priority
?
268 Views
Last Modified: 2013-01-24
Hello Experts!
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;
}

Open in new window

0
Comment
Question by:panJames
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38810261
Your friend class declaration should not have braces at the end
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.
0
 
LVL 22

Accepted Solution

by:
ambience earned 1200 total points
ID: 38810318
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
  };
}

Actually, the reason it fails is because a friend function is not permitted in local class. This is what the standard says

A function can be defined in a friend declaration of a class if and only if the class is a non-local class

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.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 38810322
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:
void f(); // prototype

void z() {
	class B {//we define class within method... weird but legal
		friend void ::f(); //ok
	};
}

void f() { /*do something*/ }

Open in new window

Hope that helps,

ZOPPO
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:ambience
ID: 38810337
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
  };
}
0
 

Author Comment

by:panJames
ID: 38813447
@ambience


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
0
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 800 total points
ID: 38813488
Hi panJames,

a local class is a class which is declared within another class or a function, i.e.:
class A // none-local class
{
 class B{}; // local class
};

 void f() {
  class C{}; // local class
}

Open in new window

Next: friend void h() { } declares and implements a method in the class. The friend keyword is meaningless here since a method always has access to private members of their class.

What you can do declaring a global function as friend is to allow a specific function to access private members, something like this:
class C {
	int x; // private
	friend void f();
};

void f()
{
	C c;
	c.x = 1; // OK
}

void h()
{
	C c;
	c.x = 1; // Error: cannot access private member
}

Open in new window

Hope that helps,

ZOPPO
0
 
LVL 22

Expert Comment

by:ambience
ID: 38813547
>>  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

class A { public: class B { }; };

A::B b;

Open in new window


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();
	}
};

Open in new window


>> 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.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 38813567
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 :(
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question