//
// main.cpp
// test
//
// Created by 杜国超 on 17/9/9.
// Copyright © 2017年 杜国超. All rights reserved.
//
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
class CObjB;
class CObjA
{
public:
CObjA() { cout << "CObjA Constructor..." << endl; }
~CObjA() { cout << "CObjA Destructor..." << endl; }
shared_ptr<CObjB> m_pb; // 在A中引用B
};
class CObjB
{
public:
CObjB() { cout << "CObjB Constructor..." << endl; }
~CObjB() { cout << "CObjB Destructor..." << endl; }
shared_ptr<CObjA> m_pa; // 在B中引用A
};
int main() {
shared_ptr<CObjA> tmpPa = make_shared<CObjA>();
shared_ptr<CObjB> tmpPb = make_shared<CObjB>();
tmpPa->m_pb = tmpPb;
tmpPb->m_pa = tmpPa;
std::cout << "CObjA referencr num:" << tmpPa.use_count() << endl;
std::cout << "CObjB referencr num:" << tmpPb.use_count() << endl;
// tmpPa->m_pb.reset();
// tmpPb->m_pa.reset();
// std::cout << "CObjA referencr num:" << tmpPa.use_count() << endl;
// std::cout << "CObjB referencr num:" << tmpPb.use_count() << endl;
}
//
// main.cpp
// test
//
// Created by 杜国超 on 17/9/9.
// Copyright © 2017年 杜国超. All rights reserved.
//
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
class CObjB;
class CObjA
{
public:
CObjA() { cout << "CObjA Constructor..." << endl; }
~CObjA() { cout << "CObjA Destructor..." << endl;}
void Say() {cout << "CObjA Say..." << endl; }
shared_ptr<CObjB> GetPb() {
return m_pb.lock();
}
public:
weak_ptr<CObjB> m_pb; // 在A中引用B
};
class CObjB
{
public:
CObjB() { cout << "CObjB Constructor..." << endl; }
~CObjB() { cout << "CObjB Destructor..." << endl; }
void Say() {cout << "CObjB Say..." << endl; }
shared_ptr<CObjA> GetPa() {
return m_pa.lock();
}
public:
weak_ptr<CObjA> m_pa; // 在B中引用A
};
int main() {
shared_ptr<CObjA> tmpPa = make_shared<CObjA>();
shared_ptr<CObjB> tmpPb = make_shared<CObjB>();
tmpPa->m_pb = tmpPb;
tmpPb->m_pa = tmpPa;
std::cout << "CObjA referencr num:" << tmpPa.use_count() << endl;
std::cout << "CObjB referencr num:" << tmpPb.use_count() << endl;
if (tmpPa->GetPb()!= NULL){
tmpPa->GetPb()->Say();
}
if (tmpPb->GetPa()!= NULL){
tmpPb->GetPa()->Say();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有