By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,707 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

forward declare member function so that it can be friend function

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
<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 discussion thread is closed

Replies have been disabled for this discussion.