日期:2014-05-16 浏览次数:20635 次
#include <iostream>
template<class T>
class Slave
{
public:
Slave();
Slave(T *p_obj);
void set_master(T *p_obj);
void print();
private:
T* mp_obj;
};
class Master
{
public:
Master();
void print();
void call();
private:
unsigned long spare_;
Slave<Master> slave_;
};
template<class T>
Slave<T>::Slave()
{
mp_obj = NULL;
}
template<class T>
Slave<T>::Slave(T *p_obj)
{
mp_obj = p_obj;
}
template<class T>
void Slave<T>::set_master(T *p_obj)
{
mp_obj = p_obj;
}
template<class T>
void Slave<T>::print()
{
printf("Address of master is 0x%p\n\n", mp_obj);
mp_obj->print();
}
Master::Master() : slave_()
{
slave_.set_master(this);
}
void Master::print()
{
printf("Address of slave_ is 0x%p\n\n", &slave_);
}
void Master::call()
{
slave_.print();
}