mardi 11 avril 2023

Cleanly Passing a Dependency Using Smart Pointer

I am in the early stages of learning "modern" C++ and would like to ask what the "best-practice" is for providing a dependency to a consumer whilst using smart pointers for management.

I would like it to be the case that a change to the object state referenced by the smart pointer, is reflected in the consumer class.

I also don't want the consumer class to know about the smart pointer as I feel this makes the interface/constructor less clear. However maybe this is misinformed?

The example below does what I want using aliases, but is it the "right" way to achieve my goals using "modern" C++? Are there alternative approaches / idioms I could be aware of?

Header

class Widget {
public:
    int cost { 0 };
    virtual ~Widget();
};

class WidgetConsumer {
public:
    Widget& _widget;
    explicit WidgetConsumer(Widget&  widget);
    virtual ~WidgetConsumer();
};

Source

#include <iostream>
#include "widgets.h"

int main() {
    std::unique_ptr<Widget> widgetB = std::make_unique<Widget>();
    WidgetConsumer widgetConsumerB(*widgetB);
    widgetB->cost = 23;
    std::cout << "widget.cost=" << widgetB->cost << std::endl;
    std::cout << "widgetConsumer._widget.cost=" << widgetConsumerB._widget.cost << std::endl;
}

WidgetConsumer::WidgetConsumer(Widget &widget) : _widget(widget) {
}

WidgetConsumer::~WidgetConsumer() {
    std::cout << "WidgetConsumer destroyed" << std::endl;
}

Widget::~Widget() {
    std::cout << "Widget destroyed" << std::endl;
}

Output

widget.cost=23
widgetConsumer._widget.cost=23
WidgetConsumer destroyed
Widget destroyed

Aucun commentaire:

Enregistrer un commentaire