473,399 Members | 2,774 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,399 software developers and data experts.

Question about friend member functions


Hello,

I have two classes, say A and B, defined in header files "A.hh" and
"B.hh," respectively. The definition of class B looks like

#ifndef _B_HH_
#define _B_HH_

#include "A.hh"
// other includes here

class B {
A a;
int f (/*lots of parameters*/);
};

#endif

I'd like B::f to be a friend of class A. Is it possible to do this? If
I write

#ifndef _A_HH_
#define _A_HH_

#include "B.hh"

class A {
friend int B::f (/*lots of parameters*/);
// some other stuff
};

the compiler complains when it gets to the "A a;" line included from
B.hh, saying class A is not defined there yet. Moreover, the parameters
of B::f are completely irrelevant to class A, so I would not like to
include the corresponding header files in A.hh just for them. Is there
any solution to tackle this?

Thanks,

Gergo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
2 1630
Gergely Korodi wrote:
I have two classes, say A and B, defined in header files "A.hh" and
"B.hh," respectively. The definition of class B looks like

#ifndef _B_HH_
#define _B_HH_

#include "A.hh"
// other includes here

class B {
A a;
int f (/*lots of parameters*/);
};

#endif

I'd like B::f to be a friend of class A. Is it possible to do this? If
I write

#ifndef _A_HH_
#define _A_HH_

#include "B.hh"

class A {
friend int B::f (/*lots of parameters*/);
// some other stuff
};

the compiler complains when it gets to the "A a;" line included from
B.hh, saying class A is not defined there yet. Moreover, the parameters
of B::f are completely irrelevant to class A, so I would not like to
include the corresponding header files in A.hh just for them. Is there
any solution to tackle this?


I think, in general there is no direct solution because you're trying
to declare a member of a class without defining a class. That would
be a "forward declaration of a member", which is not allowed.

You either have to declare the whole class B a friend or work around
having to declare a friend. A global function could be declared outside
the class and then declared a friend of both classes, and you could just
call it in B::f if you need to.

Victor

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #2
> I'd like B::f to be a friend of class A. Is it possible to do this? If
I write


If I understand what you are saying try using an intermediate helper
function that can be a forward declaration

a.h

#ifndef _A_HH_
#define _A_HH_

class B;
int helper(B *b);

class A {
public:
A::A(B *b) : b_(b) {}
void A::doSomethingToB() {int i = helper(b_);}
private:
// some other stuff
B* b_;
};
#endif

b.h

ifndef _B_HH_
#define _B_HH_

#include <iostream>
#include "a.h"

class B
{
public:
B::B() : a(this) {}
void doSomethingWithA() {a.doSomethingToB();}
private:
friend int helper(B *b);
A a;
int f (/*lots of parameters*/) {std::cout << "B::f" << std::endl;return
0;}
};
#endif

main.cpp

#include "b.h"

int helper(B *b)
{
return b->f();
}

int main()
{
B b;
b.doSomethingWithA();
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
5
by: pat270881 | last post by:
hello, i should implement this class: namespace test_1 { class statistician { public: // CONSTRUCTOR
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
1
by: Jess | last post by:
Hello, I am trying to define a friend function for my class as follows: #include<iostream> using namespace std; class B;
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...
18
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.