main.h
class B;
class A;
class A
{
public:
//构造函数
A(){x=123;}
//析构函数
~A(){};
//功能函数
public:
void ATest(void) {printf("%d\n",x);}
void Set(B* b){m_b=b;}
//静态函数
public:
static B* GetInstance(){return m_b;}
//静态变量
protected:
static B * m_b;
//私有变量
private:
int x;
};
class B
{
public:
//构造函数
B(){y=456;}
//析构函数
~B(){};
//功能函数
public:
void BTest(void) {printf("%d\n",y);}
void Set(A* a){m_a=a;}
//静态函数
public:
static A* GetInstance(){return m_a;}
//静态变量
protected:
static A * m_a;
//私有变量
private:
int y;
};
main.cpp
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include "main.h"
B* A::m_b=NULL;
A* B::m_a=NULL;
int main (void)
{
A a1;
B b1;
a1.Set(&b1);
b1.Set(&a1);
a1.GetInstance()->BTest();
b1.GetInstance()->ATest();
getchar();
return 0;
}