Linked Questions
11 questions linked to/from C++ - passing references to std::shared_ptr or boost::shared_ptr
467 votes
17 answers
261k views
When should I use pointers instead of references in API-design?
I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an API? Naturally some ...
113 votes
5 answers
111k views
Passing shared pointers as arguments
If I declare an object wrapped in a shared pointer: std::shared_ptr<myClass> myClassObject(new myClass()); then I wanted to pass it as an argument to a method: DoSomething(myClassObject); //...
66 votes
5 answers
23k views
The cost of passing by shared_ptr
I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function arguments. Consider the following: class Dataset {...} void f( shared_ptr< Dataset ...
26 votes
6 answers
17k views
Why shouldn't you use references to smart pointers?
I recall reading somewhere that using references to smart pointers can cause memory corruption. Is this simply because of using the reference of the smart pointer after its been destroyed? Or does ...
12 votes
4 answers
16k views
Can I cast shared_ptr<T> & to shared_ptr<T const> & without changing use_count?
I have a program that uses boost::shared_ptrs and, in particular, relies on the accuracy of the use_count to perform optimizations. For instance, imagine an addition operation with two argument ...
7 votes
2 answers
11k views
C++/CLI Wrapping a Function that Returns a std::shared_ptr
I'm currently wrapping a C++ class with C++/CLI for .NET interoperability following the standard process of holding a native pointer in a managed class. In one instance, I have a native class that has ...
10 votes
4 answers
9k views
Should I convert shared_ptr to weak_ptr when passed to a method?
there are already a couple of questions regarding this topic, but I am still not sure what to do: Our codebase uses shared_ptr at many places. I have to admit that we did not define ownership clearly ...
9 votes
3 answers
9k views
Qt using boost::shared_ptr in a signal/slot
Is it possible, and if so, how can I create a signal/slot in Qt that is a const reference to a shared_ptr? I want a signal that looks like this: void signal( shared_ptr<SomeClass> const & )...
7 votes
4 answers
3k views
How to pass a shared_ptr to a mutable object as a parameter?
I want to pass an object by smart-pointer reference to a function. The function may change the value of the referenced object, but may not change the reference itself. There are two obvious ways to ...
1 vote
3 answers
190 views
Subtle error when using temporaries to get iterators to a STL container: how to avoid it?
Let's consider this class: class X { std::map<uint32_t, uint32_t> _map; public: X() { /* Populate the map */ } std::map<uint32_t, uint32_t> getTheMap() { return _map; } }; ...
0 votes
0 answers
193 views
Can an excessive use of smart pointers lead to an increase in system kernel calls?
Can an excessive use of smart pointers (especially shared_ptr) cause an increase in kernel calls? I am trying to optimize and simplify a real-time (QNX) codebase. One focus besides memory and realtime ...