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

forward declare member function so that it can be friend function

currently, i have a private function in cat named privateFun.

i would like to have this function "private" to all except dog's action
member function.

by using the following approach, all the dog's members can access all
the cat's private members.

// cat.h
//
#ifndef CAT_H
#define CAT_H

// forward declaration.
class dog;

class cat
{
private:
friend class dog;
void privateFun() {}
};

#endif
//
// cat.h

// dog.h
//
#ifndef DOG_H
#define DOG_H

#include "cat.h"

class dog
{
private:
void action()
{
cat c;
c.privateFun();
}
};

#endif
//
// dog.h

however, i would like to have ONLY dog's action member function to
access cat's private members.

i try the following approach but can't work.
// cat.h
//
#ifndef CAT_H
#define CAT_H

// forward declaration.
class dog;

class cat
{
private:
friend void dog::action(); /* HERE IS THE CHANGES AND COMPILATION
ERROR HAPPENS HERE. */
void privateFun() {}
};

#endif
//
// cat.h

// dog.h
//
#ifndef DOG_H
#define DOG_H

#include "cat.h"

class dog
{
private:
void action()
{
cat c;
c.privateFun();
}
};

#endif
//
// dog.h

Of course, i would get the following compilation error:

c:\Documents and Settings\YC Cheok\Desktop\aaa\cat.h(10): error C2027:
use of undefined type 'dog'

However, I just cann't include the dog header file into cat. This will
introduce circular include problem.

Any advice? Can I have something like member function forward
declaration? Thank you very much

Nov 9 '06 #1
1 3251
<ya************@gmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
currently, i have a private function in cat named privateFun.

i would like to have this function "private" to all except dog's action
member function.

by using the following approach, all the dog's members can access all
the cat's private members.

// cat.h
//
#ifndef CAT_H
#define CAT_H

// forward declaration.
class dog;

class cat
{
private:
friend class dog;
void privateFun() {}
};

#endif
//
// cat.h

// dog.h
//
#ifndef DOG_H
#define DOG_H

#include "cat.h"

class dog
{
private:
void action()
{
cat c;
c.privateFun();
}
};

#endif
//
// dog.h

however, i would like to have ONLY dog's action member function to
access cat's private members.

i try the following approach but can't work.
// cat.h
//
#ifndef CAT_H
#define CAT_H

// forward declaration.
class dog;

class cat
{
private:
friend void dog::action(); /* HERE IS THE CHANGES AND COMPILATION
ERROR HAPPENS HERE. */
void privateFun() {}
};

#endif
//
// cat.h

// dog.h
//
#ifndef DOG_H
#define DOG_H

#include "cat.h"

class dog
{
private:
void action()
{
cat c;
c.privateFun();
}
};

#endif
//
// dog.h

Of course, i would get the following compilation error:

c:\Documents and Settings\YC Cheok\Desktop\aaa\cat.h(10): error C2027:
use of undefined type 'dog'

However, I just cann't include the dog header file into cat. This will
introduce circular include problem.

Any advice? Can I have something like member function forward
declaration? Thank you very much
Make sure you break up the implementation from the declaration. So you
should be able to include dog.h from cat.h as long as dog.h doesn't actually
have any code. This is untested code, but should show you how to fix the
problem:

// dog.h
class cat; // forward declaration
class dog
{
private:
void action();
cat c;
};

// cat.h
#include "dog.h"
class cat
{
private:
friend void dog::action(); // This should work now
void privateFun() {}
};

// dog.cpp
#include "dog.h"
#include "cat.h"
void dog::action()
{
c.privateFun();
}

// cat.cpp
#include "cat.h"
void cat::privateFun()
{
}


Nov 9 '06 #2

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

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
10
by: Alan Lee | last post by:
Hi i am writing a small simulation for a bunch of atoms jumping around on a surface. I know i am a crappy programmer since i am not very familiar with object oriented languages. I am woindering...
7
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward...
2
by: Kai Wu | last post by:
Hello, struct S{ friend class B; private: int i; }; class B{ B(){}
2
by: John Ratliff | last post by:
I'm having issues with forward declarations and possibly member variables. Can you declare a member variable and pass it parameters. class x { private: y obj(this); } Is that valid? I'm...
3
by: Till Crueger | last post by:
Hi, I am trying to implement a tree in C and I have the folowing code: struct inner { struct node *left; struct node *right; }; struct leaf {
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
8
boxfish
by: boxfish | last post by:
Hi everyone, I'm working on a 3d maze game, and I just messed up the classes so it won't compile, and I need help sorting it out. There's a class Maze, whose members are, among other things, a list...
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: 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...
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
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...
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.