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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 classes that both require each other

Status
Not open for further replies.

bdaviesrules

Technical User
Dec 22, 2004
8
CA
I have 2 classes, lets say Class A and Class B. Class A has member functions that are passed objects of Class B, and Class B has member functions that require objects of Class A.
What do I do?

thanks in advance for any help.
 
Use forward declarations.
Code:
// A.h
#ifndef A_H
#define A_H

class B;

class A
{
    void DoIt(const B& b);
    void Again(B b);
};
#endif // A_H
Code:
// B.h
#ifndef B_H
#define B_H

class A;

class B
{
    void DoIt(const A& a);
    void Again(A a);
};
#endif // B_H
Code:
// A.cpp

#include "A.h"
#include "B.h"

void A::DoIt(const B& b) { /* use b */ }
void A::Again(B b) { /* use b */ }
Code:
// B.cpp

#include "B.h"
#include "A.h"

void B::DoIt(const A& a) { /* use a */ }
void B::Again(A a) { /* use a */ }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top