473,657 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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++.m oderated. First time posters: Do this! ]
Jul 22 '05 #1
2 1658
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++.m oderated. 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::doSomethingT oB() {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 doSomethingWith A() {a.doSomethingT oB();}
private:
friend int helper(B *b);
A a;
int f (/*lots of parameters*/) {std::cout << "B::f" << std::endl;retur n
0;}
};
#endif

main.cpp

#include "b.h"

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

int main()
{
B b;
b.doSomethingWi thA();
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. 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
17861
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. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
51
4269
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, a method (event) needs to be able to invoke methods of the second class. With static classes its possible but this is not desirable. There's obviouly some visibility problem I am not familiar with. It is not a parent-child relationship since...
12
3245
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 private. I have placed "friend void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to execute CA::Run_A(), but compiler shows an error saying that it is the violation to access private function. I don't understand why because friend...
5
3607
by: pat270881 | last post by:
hello, i should implement this class: namespace test_1 { class statistician { public: // CONSTRUCTOR
4
2047
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 other things. What would be a good design to accomplish these goals? Here are the ideas that I have come up with: Class X = class with data that I want to put into csv. 1. Nested Function Object use a nested function object to do the conversion.
7
2110
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 BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
1
1683
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
2713
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 public data members. Don't use friends, except to avoid or . Consider the following scenarios: Scenario 1:
18
2499
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 container. I have written both these operators as non-friend functions.
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7347
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.