Skip to main content

Questions tagged [smart-pointer]

For questions about the usage of the smart pointer construct.

Filter by
Sorted by
Tagged with
-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
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; ~...
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
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 ...
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
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> ...
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
3k 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
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 ...
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
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....
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
  • 584
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 ...
lugge86's user avatar
  • 445
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 ...
Nathan's user avatar
  • 1,309
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 ...
el.pescado - нет войне's user avatar
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 ...
Oebele's user avatar
  • 215
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 ...
Nick's user avatar
  • 305
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 ...
dawsonc623's user avatar
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 ...
CheshireChild's user avatar
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&...
user avatar
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 ...
Matthew James Briggs's user avatar
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<...
VF1's user avatar
  • 1,881
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 ...
Romain's user avatar
  • 117
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 ...
twid's user avatar
  • 101
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() ...
mike30's user avatar
  • 2,818
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 ...
adanselm's user avatar
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 ...
Vector's user avatar
  • 3,231
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 ...
Louis Jackman's user avatar
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 ...
ronag's user avatar
  • 1,189
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?
That Realtor Programmer Guy's user avatar
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 ...
Gulshan's user avatar
  • 9,502