Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

teo classes that reference each other

Status
Not open for further replies.

bobby98762001

Programmer
Oct 3, 2004
1
IE
I want to create two classes that contain member functions where each meber function references the other class
eg
#include "A.h"
Class A{
private:
int a;
public:
void func(B b){};
}

#include "B.h"
Class B{
private:
int b;
public:
void func(A a){};
}

I know something is needed to reference each other but i don't know what it is...can anyone help me. in the header files i also use #ifndef etc to avoid the same header file being added more than once.
what do i have to do to allow me to reference as shown above
 
> what do i have to do to allow me to reference as shown above
One of them will have to be a pointer or a reference to the other class, to break the cycle of mutual dependency.

Example
Code:
class B;  // Forward declaration of a class

class A{
 private:
  int a;
 public:
  void func(B &b){};
  void func2(B *b){};
};

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top