-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexampleA.cpp
More file actions
38 lines (26 loc) · 979 Bytes
/
exampleA.cpp
File metadata and controls
38 lines (26 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <memory>
#include <chrono>
#include <thread>
#include "exampleB.h"
#include "exampleA.h"
A::A()
{
std::cout<<"A cons"<<std::endl;
//Bptr = std::make_shared<B>(shared_from_this()); // can not be called inside of the constructor
// std::thread([this](){
// std::this_thread::sleep_for (std::chrono::seconds(1));
// Bptr = std::make_shared<B>(shared_from_this());
// }).detach();
//std::this_thread::sleep_for (std::chrono::seconds(3));
}
void A::foo()
{
Bptr = std::make_shared<B>(shared_from_this()); // can not be called inside of the constructor
//It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by std::shared_ptr (in particular, shared_from_this cannot be called during construction of *this).
// terminate called after throwing an instance of 'std::bad_weak_ptr
}
void A::Acallback()
{
std::cout<<"Acallback"<<std::endl;
}