mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-16 20:27:08 +03:00
39 lines
672 B
C++
39 lines
672 B
C++
//
|
|
// 5.3.weak.ptr.cpp
|
|
// chapter 05 start pointers and memory management
|
|
// modern c++ tutorial
|
|
//
|
|
// created by changkun at changkun.de
|
|
// https://github.com/changkun/modern-cpp-tutorial
|
|
//
|
|
|
|
|
|
#include <iostream>
|
|
#include <memory>
|
|
|
|
class A;
|
|
class B;
|
|
|
|
class A {
|
|
public:
|
|
std::shared_ptr<B> pointer;
|
|
~A() {
|
|
std::cout << "A was destroyed" << std::endl;
|
|
}
|
|
};
|
|
class B {
|
|
public:
|
|
std::shared_ptr<A> pointer;
|
|
~B() {
|
|
std::cout << "B was destroyed" << std::endl;
|
|
}
|
|
};
|
|
int main() {
|
|
std::shared_ptr<A> a = std::make_shared<A>();
|
|
std::shared_ptr<B> b = std::make_shared<B>();
|
|
a->pointer = b;
|
|
b->pointer = a;
|
|
|
|
return 0;
|
|
}
|