Skip to main content

Questions tagged [smart-pointer]

For questions about the usage of the smart pointer construct.

-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'...
T.E.D.'s user avatar
  • 1,069
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; ~...
Martin Perry's user avatar
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 ...
Luke B's user avatar
  • 147
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. ...
wcochran's user avatar
  • 171
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 ...
hacker emzeze's user avatar
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 ...
Marvg's user avatar
  • 33
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 ...
Kevin's user avatar
  • 844
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 ...
amd's user avatar
  • 59
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> ...
AnInquiringMind's user avatar
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: ...
Marco Stramezzi's user avatar
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 | ...
pluczak's user avatar
  • 13
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 ...
marcin's user avatar
  • 79
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 ...
user695652's user avatar
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....
Lasse Kliemann's user avatar
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 ...
bbalchev's user avatar
  • 594

15 30 50 per page