![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
two classes, call each others functions - how?!!
Hi,
I'm just trying to get two classes to succesfully be able to call functions within each others class. So I effectively have:
// in A's header file
class A {
public:
B *b;
void a_function(); // in this function, there's the line b->b_function2
void a_function2();
};
// in B's header file
class B {
public:
A *a;
void b_function(); // in this function, there's the line a->a_function2
void b_function2();
};Any ideas? This has troubled me for a while and is a very difficult problem to google!! |
|
|
|
|
|
#2 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4
![]() |
Re: two classes, call each others functions - how?!!
Google for "circular dependency".
__________________
http://www.klarre.se |
|
|
|
|
|
#3 |
|
Programmer
|
Re: two classes, call each others functions - how?!!
I'm confused. Does that mean there is no possible way of doing this at all? Because that's what my google searching seems to suggest. How do people usually get round this problem? I tried making all my classes basically implement interfaces and include an interface file, but that gave me a linker error. Is there no way to do forward declaration of methods as well as classes?
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: two classes, call each others functions - how?!!
Use forward declarations and #include guards.
The forward declarations: in A's header file put a line "class B;" before the declaration of class A, and in B's header file place a line "class A;" before the declaration of class B. Do not #include the headers within each other. #include both header files within the .cpp files. Also put an include guard in both files, to avoid circular inclusions (A.h #including B.h #including A.h ..... ad infinitum) The net effect is that A's header will look like this. B's will be in a similar form. #ifndef SOME_MACRO_UNIQUE_TO_A_HEADER
#define SOME_MACRO_UNIQUE_TO_A_HEADER
class B;
class A {
public:
B *b;
void a_function(); // in this function, there's the line b->b_function2
void a_function2();
};
#endif |
|
|
|
|
|
#5 |
|
Not a user?
Join Date: Sep 2007
Posts: 272
Rep Power: 2
![]() |
Re: two classes, call each others functions - how?!!
Wouldn't making each class a friend class to the other do the job for just calling functions?
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: two classes, call each others functions - how?!!
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
Re: two classes, call each others functions - how?!!
The friend keyword would allow the classes to access all of each others public, private and protected data and functions. but would not resolve this cyclic dependancy issue.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using internal classes in STL functions | manannan | C++ | 4 | May 31st, 2006 1:25 AM |
| What is the point of pure virtual functions? | aznluvsmc | C++ | 12 | Apr 14th, 2006 6:48 PM |
| Could some please explain classes to me... | TCStyle | C++ | 10 | Feb 20th, 2006 3:51 PM |
| Exporting Functions | victorsk | Visual Basic | 1 | May 18th, 2005 2:32 PM |
| User-defined creatNode and deleteNode functions for a doubly-linked list | jgs | C | 2 | Apr 28th, 2005 8:53 AM |