![]() |
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: :
Any ideas? This has troubled me for a while and is a very difficult problem to google!! |
Re: two classes, call each others functions - how?!!
Google for "circular dependency".
|
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?
|
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 |
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?
|
Re: two classes, call each others functions - how?!!
Quote:
|
Re: two classes, call each others functions - how?!!
Quote:
|
| All times are GMT -5. The time now is 4:19 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC