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