Solved

Friend classes and methods

Posted on 2013-01-23
1
394 Views
Last Modified: 2013-01-23
Hello experts!

To declare method of class A a friend of class B this method needs to be declared above declaration of class B.

To declare entire class A a friend of class B class A does not need to be declared above declaration of class B.

Why does compiler works this way, what logic is behind it?

#include <iostream>
using namespace std;

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

int main() {
  X xobj;
  Y yobj;
  yobj.print(xobj);
}

Open in new window


#include <iostream>
using namespace std;

class X {
  int a, b;
  friend class F;
public:
  X() : a(1), b(2) { }
};

class F {
public:
  void print(X& x) {
    cout << "a is " << x.a << endl;
    cout << "b is " << x.b << endl;
  }
};

int main() {
  X xobj;
  F fobj;
  fobj.print(xobj);
}

Open in new window

0
Comment
Question by:panJames
1 Comment
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 38809528
Hi panJames,

it is not possible to use something with friend which the compiler doesn't know yet. The second sample with friend class F; works because the compiler treats this as a forward declaration of the class F. But there's no way to do something similar to a forward declaration for class methods, so the first sample can only work if Y is completeley with all methods nad members declared before X.

Hope that helps,

ZOPPO
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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