Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Object Ownership

auto_ptr<CPClass> a(new CPClass);
auto_ptr<CPClass> b(a); //copy the a

printf("%p", a.get()); // prints 0
printf("%p", b.get()); // prints valid pointer
a.drawio.svg
struct B;
struct A{
...
std::shared_ptr<B> b;
}
struct B{
...
std::shared_ptr<A> a;
}
int main(){
std::shared_ptr<A> a = std::make_shared<A>();
std::shared_ptr<B> a = std::make_shared<B>();
a->b = b;
b->a = a;
}
struct B;
struct A{
...
std::shared_ptr<B> b;
}
struct B{
...
std::weak_ptr<A> a;
}
int main(){
std::shared_ptr<A> a = std::make_shared<A>();
std::shared_ptr<B> a = std::make_shared<B>();
a->b = b;
b->a = a;
}

About KDAB


2 Comments

28 - Jul - 2023

Serhan Kars

std::shared_ptr<A> a = std::make_shared<A>();
std::shared_ptr<B> a = std::make_shared<B>();
The second variable's name should be b.

28 - Jul - 2023

Ilya Doroshenko