Category : shared-ptr

I have recently started learning shared pointers and need some help. #include <bits/stdc++.h> using namespace std; class A { public: int a; A (int x) { a = x; } int print() { return a; } }; int main() { shared_ptr<vector<shared_ptr<A>>> vec1 = make_shared<vector<shared_ptr<A>>>(1); (*vec1)[0] = make_shared<A>(50); shared_ptr<vector<shared_ptr<A>>> vec2 = make_shared<vector<shared_ptr<A>>>(); vec2->push_back((*vec1)[0]); for (shared_ptr<A> obj ..

Read more

Suppose I have a code like this: class Foo { private: int x; public: void* operator new(size_t size); void operator delete(void* p); }; int main() { auto ptr = std::shared_ptr<Foo>(new Foo()); return 0; } The shared_ptr will create separate control-block and object-block. I suppose the memory for the object block will be created using the ..

Read more

Possible Duplicate: Here is the testing code. f() returns a copy of vector v. t is a reference to f()[0]. #include <iostream> #include <memory> #include <vector> struct Test {}; std::vector<std::shared_ptr<Test>> v; std::vector<std::shared_ptr<Test>> f() { return v; } int main() { v.push_back(std::make_shared<Test>()); auto &t = f()[0]; std::cout << "count from f()[0]: " << f()[0].use_count() << std::endl; ..

Read more

class B; class C; class B { public: B() { cout<<"B created"<<endl; } ~B() { cout<<"B destroyed"<<endl; } shared_ptr<C*> ptrc; }; class C { public: C() { cout<<"C created"<<endl; } ~C() { cout<<"C destroyed"<<endl; } }; int main() { shared_ptr<B*> bb = make_shared<B*>(new B); bb->ptrc = make_shared<C*>(new C);// this line gives error } error: a.cpp: ..

Read more

In the code below, I understand it to mean that myBigStruct is inheriting multiple things. The obvious part is it’s inheriting 2 structs (mySmallStruct and someOtherStruct). But I am not sure what it means to inherit from a shared pointer. (My understanding is that std::enabled_shared_from_this returns a shared pointer to a myBigStruct – presumably a ..

Read more