#include <iostream>
#include <memory>
#include <atomic>
class C
{
public:
C();
~C();
int my_temp;
void test();
};
C::C()
:my_temp(0)
{
printf("C构造函数\n");
}
C::~C()
{
}
void C::test()
{
printf("C::test\n");
}
namespace metasp
{
namespace net
{
//class C;
class B
{
public:
B(std::shared_ptr<C>tmp,int tmp1);
virtual ~B();
virtual void test2()
{
printf("virtual void test2\n");
}
std::shared_ptr<C> pmyC;
//std::atomic<int> mytmp1;
int mytmp1;
void test3()
{
printf("test3()\n");
}
};
}
}
using namespace metasp::net;
using namespace std;
B::B(std::shared_ptr<C>tmp,int tmp1)
:pmyC(new C()),
mytmp1(tmp1)
{
printf("B构造函数\n");
pmyC->test();
printf("B::mytmp1 = %d\n",(mytmp1));
printf("B::mytmp1 = %p\n",(&B::mytmp1));//类的偏移地址
printf("this->mytmp2 = %d\n",(this->mytmp1));
test3();
this->test3();
B::test3();
}
B::~B()
{
}
using NewCallback = std::function<void (int tmp)> ;
using DefaultFunction = std::function<void()>;
class A : public metasp::net::B
{
public:
A(std::shared_ptr<C>tmp,int tmp1);
~A();
void test2();
void test2(int a);
void Event1();
void Event2(int a);
void setFunc(const DefaultFunction func);
void setNewCallback( const NewCallback callback);
// std::shared_ptr<C> pmyC;
DefaultFunction messageCallback;
NewCallback messageCallback1;
};
A::A(std::shared_ptr<C>tmp,int tmp1)
:B(tmp,tmp1)
{
printf("A构造函数\n");
pmyC->test();
this->setFunc(std::bind(&A::Event1,this));
this->setNewCallback(std::bind(&A::Event2,this,std::placeholders::_1));
}
A::~A()
{
}
void A::test2()
{
printf("A::test2\n");
}
void A::test2(int a)
{
printf("A::test2(int a) a=%d\n",a);
}
void A::setNewCallback( const NewCallback callback)
{
messageCallback1 = callback;
}
void A::setFunc(const DefaultFunction func)
{
messageCallback = func;
}
void A::Event1()
{
printf("A::Event1()\n");
}
void A::Event2(int tmp)
{
printf("A::Event2() tmp = %d\n",tmp);
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
std::shared_ptr<C> pmyC;
int mytmp5 = 5;
A *pA = new A(pmyC,mytmp5);
pA->test2();
pA->test2(7);
pA->Event1();
pA->Event2(mytmp5);
printf("hello world\n");
return 0;
}
C++ 测试 DEMO
最新推荐文章于 2023-04-27 20:22:25 发布