运行结果:
./main
this is dump by test
this dump by second
head1.hpp
#ifndef __HEAD1__
#define __HEAD1__
#include <iostream>
class Second;
class Test{
public:
Test();
~Test();
int reference_myself(Test *test);
int test_call();
int test_dump();
int set_second(Second *second);
Test *test;
Second *second;
};
#endif
head1.cpp
#include "head1.hpp"
#include "head2.hpp"
Test::Test()
{
}
Test::~Test()
{
}
int Test::reference_myself(Test *test){
this->test = test;
return 0;
}
int Test::test_call()
{
second->second_dump();
return 0;
}
int Test::set_second(Second *second){
this->second = second;
return 0;
}
int Test::test_dump()
{
std::cout << "this is dump by test " << std::endl;
return 0;
}
head2.h
#ifndef __HEAD2__
#define __HEAD2__
#include "head1.hpp"
class Second{
public:
Second(Test *test);
~Second();
int reference_myself(Second *second);
int second_call();
int second_dump();
Test *test;
Second *second;
};
#endif
head2.cpp
#include "head1.hpp"
#include "head2.hpp"
Second::Second(Test *test){
this->test = test;
}
Second::~Second(){
}
int Second::reference_myself(Second *second){
//this->second = second;
this->second = second;
return 0;
}
int Second::second_call(){
test->test_dump();
return 0;
}
int Second::second_dump(){
std::cout << "this dump by second " << std::endl;
return 0;
}
#include "head1.hpp"
#include "head2.hpp"
void testfunc()
{
#if 1
Test *test = new Test();
Second *second = new Second(test);
test->reference_myself(test);
test->set_second(second);
second->reference_myself(second);
//second->set_test(test);
test->second->second_call();
second->test->test_call();
#endif
}
int main(int argc, char *argv[])
{
testfunc();
return 0;
}