forward declare member function so that it can be friend function

Y

yancheng.cheok

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
 
J

Jim Langston

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()
{
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top