Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   two classes, call each others functions - how?!! (http://www.programmingforums.org/showthread.php?t=15234)

deanosrs Feb 22nd, 2008 11:10 AM

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();
};

The problem I have at the moment is that I have to include in each header file, the other header file, which it doesn't like. I've also tried forward class definitions, but this doesn't work either, because the line class B; in A's header file isn't sufficient to tell A that B has a function called b_function2().

Any ideas? This has troubled me for a while and is a very difficult problem to google!!

Klarre Feb 22nd, 2008 11:23 AM

Re: two classes, call each others functions - how?!!
 
Google for "circular dependency".

deanosrs Feb 22nd, 2008 3:20 PM

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?

grumpy Feb 22nd, 2008 3:41 PM

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

Keep in mind that forward references only allow declarations of pointers and references to the forward-declared class. If you need to call member functions, a forward definition is insufficient. it is necessary to have a complete definition (i.e. in code that calls a member function of A, it is necessary to #include A's header file).

Jabo Feb 22nd, 2008 7:51 PM

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?

grumpy Feb 23rd, 2008 12:46 AM

Re: two classes, call each others functions - how?!!
 
Quote:

Originally Posted by Jabo (Post 141380)
Wouldn't making each class a friend class to the other do the job for just calling functions?

No.

Seif Feb 23rd, 2008 4:53 AM

Re: two classes, call each others functions - how?!!
 
Quote:

Originally Posted by Jabo (Post 141380)
Wouldn't making each class a friend class to the other do the job for just calling functions?

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.


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