class C;
class B : public std::enable_shared_from_this<B>
{
public:
~B(){ cout << "~B" << endl; }
void SetPC(std::shared_ptr<C>& pc){ _pc = pc; }
private:
std::shared_ptr<C> _pc;
};
class C : public std::enable_shared_from_this<C>
{
public:
~C(){ cout << "~C" << endl; }
void SetPB(std::shared_ptr<B>& pb){ _pb = pb; }
private:
std::shared_ptr<B> _pb;
};
int main()
{
std::shared_ptr<C> pc = std::make_shared<C>();
std::shared_ptr<B> pb = std::make_shared<B>();
pc->SetPB(pb);
pb->SetPC(pc);
return 0;
}
class C : public std::enable_shared_from_this < C >
{
public:
~C()
{
std::cout << "~C" << std::endl;
}
int32_t Decode(const char* data, size_t)
{
return 0;
}
void SetDecoder(std::function<int32_t(const char*, size_t)> decoder)
{
_decoder = decoder;
}
private:
std::function<int32_t(const char*, size_t)> _decoder;
};
int main()
{
{
std::shared_ptr<C> pc = std::make_shared<C>();
auto decoder = std::bind(&C::Decode, pc, std::placeholders::_1, std::placeholders::_2);
pc->SetDecoder(decoder);
}
// C不能正确析构 因为存在自引用
return 0;
}
class Session : public std::enable_shared_from_this < Session >
{
public:
~Session()
{
std::cout << "~C" << std::endl;
}
void Start()
{
// 进行一些异步调用
// 如 _socket.async_connect(..., boost::bind(&Session::ConnectCompleted, this), boost::asio::placeholders::error, ...)
}
void ConnectCompleted(const boost::system::err_code& err)
{
if(err)
return;
// ... 进行处理
// 如 _socket.async_read(..., boost::bind(&Session::ReadCompleted, this), boost::asio::placeholders::error, ...)
}
void Session::ReadComplete(const boost::system::error_code& err, size_t bytes_transferred)
{
if (err || bytes_transferred == 0)
{
DisConnect();
return;
}
// 处理数据 继续读
// ProcessData();
// _socket.async_read(...)
}
private:
std::function<int32_t(const char*, size_t)> _decoder;
};
int main()
{
{
std::shared_ptr<Session> pc = std::make_shared<Session>();
pc->Start();
}
return 0;
}
((A*)nullptr)->Func();
class Test
{
public:
void Say()
{
std::cout << "Say Test" << std::endl;
}
void Set(int data)
{
_data = data;
}
private:
int _data;
};
int main()
{
// 运行成功
((Test*)nullptr)->Say();
// 运行会崩掉,尝试访问空指针所指内存(_data)
((Test*)nullptr)->Set(1);
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有