2,871 questions
Best practices
0
votes
9
replies
218
views
Is it worth using a unique pointer for a Huffman Tree
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
Duplicating a derived class stored in a smart pointer [duplicate]
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
How do std::unique_ptr and std::shared_ptr differ in C++?
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
C++ Cast base class unique_ptr to derived class unique_ptr [duplicate]
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
Can I use unique_ptr with inherited classes?
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
How should I store a dyn FnMut in a struct?
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
Why can’t I efficiently move-construct `std::polymorphic<Base>` from `std::polymorphic<Derived>`?
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
Linkedlist create like below with shared_ptr is proper or wrong - [closed]
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?
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
How to handle return of unique_ptr in trompeloeil RETURN expectations
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
Smart pointers and polymorphism - Use in a model class is causing casting issues
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
Am I over-encouraging people to use shared_ptr's? [closed]
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
Will unique pointer free the memory? [duplicate]
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
std::vector: when is a pointer not a pointer?
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
invalid user-defined conversion from Pointer<Node> to Node*
#include <cstddef>
#include <stdexcept>
#include <cassert>
template <typename T>
class Pointer {
private:
std::ptrdiff_t offset;
std::ptrdiff_t calculateOffset(const ...