473,287 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

forward declare member function so that it can be friend function

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
1 3233
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
10
by: Alan Lee | last post by:
Hi i am writing a small simulation for a bunch of atoms jumping around on a surface. I know i am a crappy programmer since i am not very familiar with object oriented languages. I am woindering...
7
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward...
2
by: Kai Wu | last post by:
Hello, struct S{ friend class B; private: int i; }; class B{ B(){}
2
by: John Ratliff | last post by:
I'm having issues with forward declarations and possibly member variables. Can you declare a member variable and pass it parameters. class x { private: y obj(this); } Is that valid? I'm...
3
by: Till Crueger | last post by:
Hi, I am trying to implement a tree in C and I have the folowing code: struct inner { struct node *left; struct node *right; }; struct leaf {
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
8
boxfish
by: boxfish | last post by:
Hi everyone, I'm working on a 3d maze game, and I just messed up the classes so it won't compile, and I need help sorting it out. There's a class Maze, whose members are, among other things, a list...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.