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

member functions as friends - friends of each other?

Hello All,

I have the following two classes:

class testb{
private:
int b;
public:
void friend_of_testa();
};

class testa{
friend void testb::friend_of_testa();
private:
int a;
};

Here, testb's member function friend_of_testa() is a friend of testa.
So private member of testa (say int a), is accessible from testb's
friend_of_testa. This is all good.

But what if I wanted a member function of testa be a friend of class
testb as well? like the following:

class testa; //forward reference

class testb{
friend void testa::friend_of_testb();
private:
int b;
public:
void friend_of_testa();
};

class testa{
friend void testb::friend_of_testa();

private:
int a;
public:
void friend_of_testb();
};

This is not possible as testa's friend_of_testb() is unknown to the
compiler at the time of testb's class declaration.

I know I would need a forward reference for testa before testb can use
it. But how do make testa's friend_of_testb() a forword reference for
the above to work?

Thanks
Bipod

Jul 23 '05 #1
2 1650
bipod.rafi...@gmail.com wrote:
Hello All,

I have the following two classes:

class testb{
private:
int b;
public:
void friend_of_testa();
};

class testa{
friend void testb::friend_of_testa();
private:
int a;
};

Here, testb's member function friend_of_testa() is a friend of testa.
So private member of testa (say int a), is accessible from testb's
friend_of_testa. This is all good.

But what if I wanted a member function of testa be a friend of class
testb as well? like the following:

class testa; //forward reference

class testb{
friend void testa::friend_of_testb();
private:
int b;
public:
void friend_of_testa();
};

class testa{
friend void testb::friend_of_testa();

private:
int a;
public:
void friend_of_testb();
};

This is not possible as testa's friend_of_testb() is unknown to the
compiler at the time of testb's class declaration.
Of course.
I know I would need a forward reference for testa before testb can use
it. But how do make testa's friend_of_testb() a forword reference for
the above to work?


You cannot, because there is no way to forward-declare member
functions. The only thing you can do is :

class testa;

class testb
{
friend testa;

public:
void f();
};

class testa
{
friend testb::testa;
public:
void g();
};

testb::f() is a friend of testa and testa is a friend of testb. That's
the closest you can get.
Jonathan

Jul 23 '05 #2
yeah I guess so...

As an alternative, I can make the whole class as friend and vice versa.
That works fine.

Thanks

Jul 23 '05 #3

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
5
by: Nimmi Srivastav | last post by:
When I was learning C, I learned that the data type of a function name by itself is a function pointer. For example int someFunction(char* str) { .... }
8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
5
by: pat270881 | last post by:
hello, i should implement this class: namespace test_1 { class statistician { public: // CONSTRUCTOR
1
by: Dan Smithers | last post by:
Can a make a private member function a friend? The code below suggests not. I am using g++ 4.2.3 I realise that I can make the whole CFriend class a friend, or make my friend function public but...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
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...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.