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
116
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
222
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
22k
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
4k
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
937
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
217
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 ...