 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Nov 09, 2006 10:11 am Post subject: forward declare member function so that it can be friend fun |
|
|
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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|