Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2008, 10:10 AM   #1
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
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!!
deanosrs is offline   Reply With Quote
Old Feb 22nd, 2008, 10:23 AM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 288
Rep Power: 4 Klarre is on a distinguished road
Re: two classes, call each others functions - how?!!

Google for "circular dependency".
__________________
http://www.klarre.se
Klarre is online now   Reply With Quote
Old Feb 22nd, 2008, 2:20 PM   #3
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
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?
deanosrs is offline   Reply With Quote
Old Feb 22nd, 2008, 2:41 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,192
Rep Power: 5 grumpy is on a distinguished road
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).
grumpy is offline   Reply With Quote
Old Feb 22nd, 2008, 6:51 PM   #5
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 232
Rep Power: 1 Jabo is on a distinguished road
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?
Jabo is offline   Reply With Quote
Old Feb 22nd, 2008, 11:46 PM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,192
Rep Power: 5 grumpy is on a distinguished road
Re: two classes, call each others functions - how?!!

Quote:
Originally Posted by Jabo View Post
Wouldn't making each class a friend class to the other do the job for just calling functions?
No.
grumpy is offline   Reply With Quote
Old Feb 23rd, 2008, 3:53 AM   #7
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 212
Rep Power: 3 Seif is on a distinguished road
Re: two classes, call each others functions - how?!!

Quote:
Originally Posted by Jabo View Post
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.
Seif is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:02 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC