Questions tagged [smart-pointer]
For questions about the usage of the smart pointer construct.
34 questions
-1
votes
1
answer
123
views
Smart pointer class choice: Simplicity vs. right tool for the job
I'm wrestling with a design choice question here.
I've got a class that needs a couple of semaphores. Semaphores are non-movable objects. Objects of this class however need to go into a vector (there'...
0
votes
1
answer
104
views
C++ class design with shared pointers methods [closed]
I dont exactly know, how to handle the problem, where one class uses shared pointers but I want to call its method from inside other class via this pointer.
class Bar {
public:
Bar() = default;
~...
2
votes
1
answer
2k
views
How to store a vector of smart pointers, except some of them are owned by another object?
I'm making a basic platformer game. I have a Game class as well as Level class. The game object holds a pointer to the current Level object. A level currently has a std::vector of GameObject raw ...
6
votes
1
answer
2k
views
Procedure for migrating a large C++ code base to use smart pointers
I am in the process of refactoring a large C++ code (~2300 files, ~600K lines, mostly older C/C++98 style code) and there are definitely memory leaks that could be shored up using C++ smart pointers. ...
1
vote
2
answers
219
views
Develop a distributed pointer in C++
I s it possible to develop a distributed memory pointer in C++ by using operator overload?
The idea would be that you pass ip address and port to the overloaded pointer *, &, and the pointer ...
3
votes
1
answer
3k
views
C++ API design: using references vs smart pointers in a getter API
I'm trying to design an API for an object manager that registers and retrieves different related objects. When I want to retrieve an object, I can query it by its object id. I'm wondering if I should ...
8
votes
2
answers
16k
views
Should I use a unique_ptr with an array type, or a vector?
I've been out of C++ for years, last time I used it was back in gamedesign before C++11. I see all these new pointer types which seem great. But I'm unsure when and how to use them. In the old days I ...
5
votes
1
answer
7k
views
Why do we need to specify the type of data a pointer will hold, if all pointers are the same [duplicate]
Why do we need to specify the type of the data whose address, a pointer will hold, if all pointers are the same.
Since all pointers store addresses.
Also, the amount of space a pointer will require in ...
12
votes
4
answers
21k
views
Using vectors of shared pointers to objects in my C++ code to prevent object duplication
In my C++ project, I have three classes, Particle, Contact, and Network. The Network class will have N particles (std::vector<Particle> particles) and Nc contacts (std::vector<Contact> ...
5
votes
2
answers
18k
views
How to pass a mock as std::unique_ptr to Class under test
I am writing some test units using googletest and googlemock and I am stuck in a problem related to C++11 smart pointers and polymorphism.
Suppose you have those classes:
class A {
public:
...
1
vote
1
answer
3k
views
How can I copy and alter object with unique_ptr in it?
I basically have the following situation:
+------------------+
| |
| Input object |
...
6
votes
4
answers
930
views
Refactor big C++ application to prevent from bad pointer
In application which has about 1.5 mln lines of code there is using many pointers as a class members and in other places in code. Classes are usually very huge.
It is possible to change to make it ...
1
vote
1
answer
1k
views
Mocking of non-copyable objects
I find myself often in the situation where I want to mock a non-copyable object, for example a DbHandle handle.
I was going back and forth looking at different design choices, and I settled on the ...
1
vote
0
answers
216
views
Managing reference-like members with shared pointers [closed]
It is a well-known fact that there is no built-in mechanism that prevents member fields that are references from being invalidated, even if they are const. (For more background see: https://herbsutter....
4
votes
1
answer
4k
views
Unique pointer initialisation
What is the correct initialisation of a smart pointer?
std::unique_ptr<Class> ptr(std::make_unique<Class>());
or
std::unique_ptr<Class> ptr = std::make_unique<Class>();
Is ...
5
votes
2
answers
2k
views
Factories, vectors and smart pointers - Design Question
So, my Business Code needs some Objects.
It does not know how much objects it needs and it does not know the exact types (because polymorphism is involved).
For me, that sounds for a good reason to go ...
1
vote
1
answer
345
views
State Machine Memory Management Problems
My state machine handles requests and returns the next state. In the below simplification, I've got two states here, CreatedState and RunningState. Running state is the end state, and just returns ...
27
votes
5
answers
6k
views
C++: Should class own or observe its dependencies?
Say I have a class Foobar that uses (depends on) class Widget. In good ol' days, Widget wolud be declared as a field in Foobar, or maybe as a smart pointer if polymorphic behavior was needed, and it ...
3
votes
3
answers
1k
views
Possible alternatives to copy constructors
In my C++ project I am relying on some libraries that do memory management for me. I make wrapper classes, for ease of use and memory safety, for example the class below. Note that this is a much ...
9
votes
1
answer
3k
views
Key / Value store development porting to modern C++
I am developing a database server similar to Cassandra.
Development were started in C, but things became very complicated without classes.
Currently I ported everything in C++11, but I am still ...
3
votes
2
answers
844
views
C++ using shared_ptr with API [closed]
I'm building a library that generates a couple of types of objects that can be used by user code and the library. To keep track of these objects, I'd like to use shared_ptr's, so I can build in some ...
36
votes
1
answer
30k
views
raw, weak_ptr, unique_ptr, shared_ptr etc... How to choose them wisely?
There is a lot of pointers in C++ but to be honest in 5 years or so in C++ programming (specifically with the Qt Framework) I only use the old raw pointer:
SomeKindOfObject *someKindOfObject = new ...
10
votes
3
answers
7k
views
Polymorphic template container: shared_ptr vs reference_wrapper
Assuming we have two classes:
class A
{
...
}
class B : public A
{
...
}
Would it be better to write
std::deque<shared_ptr<A> > container;
or
std::deque<reference_wrapper&...
1
vote
1
answer
12k
views
C++ Templates where the type is a shared_ptr
When creating template classes in C++, if the type upon which the template will be specialized is intended to be a shared_ptr type, is it better to make T a shared_ptr type or make T a non-pointer ...
18
votes
4
answers
3k
views
Will destructing a large list overflow my stack?
Consider the following singly linked list implementation:
struct node {
std::unique_ptr<node> next;
ComplicatedDestructorClass data;
}
Now, suppose I stop using some std::unique_ptr<...
0
votes
3
answers
2k
views
C++ Chess board design and smart pointers [closed]
I wrote a Chess engine in Java and I am porting it over to C++. I am new to C++.
The idea:
I have a Board object which holds a 2-dimensionnal array of Piece objects. Queen, Rook, Bishop, etc are ...
0
votes
1
answer
192
views
confusion regarding handling of new smart pointers on stack frames?
Let me try to elaborate it.
Stack Frame: When we execute any function it create stack where all local variables and instructions reside.
And Smart Pointer: smart pointer like std::unique_ptr allows ...
33
votes
5
answers
11k
views
Why can't Java/C# implement RAII?
Question:
Why can't Java/C# implement RAII?
Clarification:
I am aware the garbage collector is not deterministic. So with the current language features it is not possible for an object's Dispose() ...
2
votes
2
answers
644
views
How do I get rid of cyclic references in this design?
I have 3 classes: Meeting, Project and Agenda.
A Project contains all sort of information + a list of meetings.
The Agenda contains a list of upcoming Meetings.
A Meeting contains some data + a list ...
4
votes
1
answer
2k
views
gtkmm manage/add vs smart pointers:
gtkmm provides lifetime management of widgets using
Gtk::Widget* aWidget = Gtk::manage(new Widget());
Gtk::Widget.add(*aWidget);
This delegates lifetime management of aWidget to its container ...
9
votes
4
answers
941
views
Is Non-Deterministic Resource-Management a Leaky Abstraction?
From what I can see, there are two pervasive forms of resource-management: deterministic destruction and explicit. Examples of the former would be C++ destructors and smart pointers or Perl's DESTROY ...
71
votes
9
answers
59k
views
std::shared_ptr as a last resort?
I was just watching the "Going Native 2012" streams and I noticed the discussion about std::shared_ptr. I was a bit surprised to hear Bjarne's somewhat negative view on std::shared_ptr and his comment ...
6
votes
2
answers
3k
views
Smart Pointers inside class vs Normal Pointers with Destructor
Regarding pointers which are members of classes. Should they be of a smart pointer type or is it enough to simply deal with them in the destructor of the class they are contained in?
89
votes
11
answers
32k
views
Why Garbage Collection if smart pointers are there
These days, so many languages are garbage collected. It is even available for C++ by third parties. But C++ has RAII and smart pointers. So what's the point of using garbage collection? Is it doing ...