Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Best practices
0 votes
9 replies
218 views

I have a file that is a Huffman Tree class. It takes a map of characters to frequencies, and constructs a Huffman Tree using Huffman Coding. The original design uses raw pointers, but of course ...
Score of 1
0 answers
109 views

Is there a way to write a function which duplicates a derived class stored in a smart pointer of the base class? That is, a function which duplicates the original, not just creating another pointer to ...
Advice
2 votes
14 replies
351 views

I’m trying to manage dynamic memory safely, but I don’t understand when to use unique_ptr or shared_ptr. #include <memory> std::unique_ptr<int> uptr = std::make_unique<int>(10); std::...
Score of 0
1 answer
224 views

I have a method in a derived class which I want to call via a unique_ptr to the base class. How do I cast my unique_ptr to the base class to allow me to call the method in the derived class? In ...
Score of -1
2 answers
350 views

Consider the following: struct A; struct B : public A; struct C : public A; class Foo { public: void someFunction() { if( cond1 ) a = new B; if( cond2 ) ...
Best practices
0 votes
2 replies
131 views

I'm trying to make a struct that has dynamic methods by storing FnMuts in the struct. The FnMuts would be a function written in a scripting language like Python or Lua, but there might be some cases ...
Score of 5
1 answer
216 views

As I understand it, std::polymorphic<T> and std::indirect<T> are const-propagating wrapper types for an owning pointer to a T object, where the target of the pointer is copied with the ...
Score of -3
1 answer
140 views

I am trying to create Linkedlist, where array is passed, and linkedlist is returned. However, there seems something wrong, and the linkedlist returned is circular. /** * Shared pointer, pass vector ...
Score of 0
1 answer
257 views

How does the implementation of C++ std::weak_ptr upgrade to std::shared_ptr and access the object without risking a use-after-free? The scenario: In a concurrent setting, you have a weak pointer to an ...
Score of 1
1 answer
170 views

Hope this message finds you well. I'm a little bit lost while using trompeloeil expectations syntax. Let's say I have this: #include <memory> class IA { public: virtual ~IA() = default; ...
Score of 3
1 answer
196 views

I'm attempting to make a game in C++ where pieces will move around a gameboard. In our team, we're trying to be good and use as much modern methodology as we can, given we've gotten rusty and now have ...
Score of 5
0 answers
271 views

In this earlier SO question: What is a smart pointer and when should I use one? Ten years ago, I gave a relative simple answer and got a bunch of upvotes. One paragraph in my answer reads: Use std::...
Score of -1
2 answers
380 views

Let's say you allocate an object and pass its address in a Windows message (posted, not sent). In the message handler, the address is recovered and used to initialize a std::unique_ptr. Will the ...
Score of 2
4 answers
325 views

I have this existing working code, which I am trying to convert to implementation: Bitmap *pbitmap = new Bitmap(L"images.png"); nWidth = pbitmap->GetWidth(); nHeight = pbitmap->...
Score of 1
2 answers
106 views

#include <cstddef> #include <stdexcept> #include <cassert> template <typename T> class Pointer { private: std::ptrdiff_t offset; std::ptrdiff_t calculateOffset(const ...

15 30 50 per page
1
2 3 4 5
192