Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > forward declare member function so that it can be friend function

Reply
Thread Tools

forward declare member function so that it can be friend function

 
 
yancheng.cheok@gmail.com
Guest
Posts: n/a
 
      11-09-2006
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

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      11-09-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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:rivateFun()
{
}




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Declare a Friend function which is a template member function of adifferent class getrakesh@gmail.com C++ 2 02-26-2008 11:32 AM
Howto declare a friend function to a nested class jdurancomas@gmail.com C++ 4 09-30-2007 03:33 PM
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM
how to declare a friend function that can access two class freegnu C++ 2 10-23-2006 09:28 AM



Advertisments