473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

friend classes and functions

Hello,

I am trying to define a friend function for my class as follows:

#include<iostream>

using namespace std;

class B;

class A{
friend void B::g(const A&);
//friend class B;
private:
int x;
public:
A(int a):x(a){}
};

class B:public A{
public:
B(int a):A(a){}
void g(const A& a){cout << a.x << endl;}
};

However, this failed with compiler error saying A::x is private and
member B::g is declared before type B is defined. This is surprising
because I've given forward declaration for B. I also declared B::g as
a friend, and so I think it should be able to access A::x. What is
going wrong here?

I then tried to replace friend void B::g(const A&) with friend class B
and then it worked. However, I'd only like B's g method to access A's
private part, not the whole B class. Is there a solution for my
problem?

Thanks,
Jess

May 20 '07 #1
1 1669
Jess wrote:
Hello,

I am trying to define a friend function for my class as follows:

#include<iostream>

using namespace std;

class B;

class A{
friend void B::g(const A&);
//friend class B;
private:
int x;
public:
A(int a):x(a){}
};

class B:public A{
public:
B(int a):A(a){}
void g(const A& a){cout << a.x << endl;}
};

However, this failed with compiler error saying A::x is private and
member B::g is declared before type B is defined. This is surprising
because I've given forward declaration for B. I also declared B::g as
a friend, and so I think it should be able to access A::x. What is
going wrong here?
You cannot make a member function a friend based on only a forward
declaration of the class.
>
I then tried to replace friend void B::g(const A&) with friend class B
and then it worked. However, I'd only like B's g method to access A's
private part, not the whole B class. Is there a solution for my
problem?
No, but what is the problem exactly? Friendship of individual member
functions is fairly pointless. Since you are writing both classes,
whether they are friends of each other of not is only of concern to you.
It makes no difference at all to the users of your class, it's only an
implementation detail for your class. So just declare B to be a friend of A.

>
Thanks,
Jess
john
May 20 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
7
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class...
8
by: Paul Cheetham | last post by:
Hi, I am writing an application with a large number of various classes, and I want some of them to have Friend access to protected members of other classes. i.e. I want class A to have access...
6
by: Philip Potter | last post by:
I have a graph data structure. It goes something like this: class GraphNode { private: friend class Graph; // successor GraphNodes stored as indices of the Graph's array int value; int next1,...
5
by: Steven T. Hatton | last post by:
This note appears in the discussion of name hiding and uniqueness: §3.3 #4 This note is item #6 in the discussion of "Point of declaration" §3.3.1 #6 What exactly do these statements mean?...
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
3
by: Rahul | last post by:
Hi Everyone, The following code works fine, class A { private: friend void sample(A& obj) { printf("in friend...\n"); printf("%d",obj.i);
1
by: Immortal_Nephi | last post by:
I want to show you an example how two classes can have relationship instead of an inheritance. The inheritance is not an option if you want to define two classes. The relationship between two...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.