I still prefer the common header file:
common.h
Code:
#ifndef COMMON_H_
#define COMMON_H_
class foo;
class bar;
#endif
foo.h
Code:
#ifndef FOO_H_
#define FOO_H_
#include "common.h"
class foo
{
bar* bptr;
int x;
public:
foo(bar* b=0, int y=0);
void setX(int);
int getX();
void setB(bar*);
bar* getB();
};
#endif
bar.h
Code:
#ifndef BAR_H_
#define BAR_H_
#include "common.h"
class bar
{
foo* fptr;
int z;
public:
bar(foo* f=0, int y=0);
void setZ(int);
int getZ();
void setF(foo*);
foo* getF();
};
#endif
foo.cpp
Code:
#include "foo.h"
foo::foo(bar* b, int y) : bptr(b), x(y) {}
void foo::setX(int y) { x = y; }
int foo::getX() { return x; }
void foo::setB(bar *b) { bptr=b; }
bar* foo::getB() { return bptr; }
bar.cpp
Code:
#include "bar.h"
bar::bar(foo* f, int y) : fptr(f), z(y) {}
void bar::setZ(int y) { z = y; }
int bar::getZ() { return z; }
void bar::setF(foo *f) { fptr=f; }
foo* bar::getF() { return fptr; }
main.cpp
Code:
#include <iostream>
#include "foo.h"
#include "bar.h"
using namespace std;
int main()
{
foo Foo(0, 10);
bar Bar(0, 100);
Foo.setB(&Bar);
Bar.setF(&Foo);
cout<<"Foo.getX() = "<<(Foo.getX())<<endl
<<"Bar.getZ() = "<<(Bar.getZ())<<endl
<<"Foo.getB()->getZ() = "<<(Foo.getB()->getZ())<<endl
<<"Bar.getF()->getX() = "<<(Bar.getF()->getX())<<endl;
}