2025-11-18 (3 Lec): Project Talk, C++ Pointer Types#

Project “Presentations”#

Lets talk about what has happened, and what will happen next.

  • State machine

  • Structural refactoring: libdoor has gotten too big

  • Test rack

  • AnalogSensor hierarchy

C++ Pointer Types#

From Smart Pointers: std::unique_ptr, std::shared_ptr (And std::weak_ptr):

Livehacking Script#

  • Start with this:

#include <vector>
#include <random>
#include <print>

class Sensor
{
public:
    virtual ~Sensor() {}
    virtual double get_value() = 0;
};

class MockSensor : public Sensor
{
public:
    MockSensor(double initial_value) : _value{initial_value} {}
    double get_value() override { return _value; }
    // for tests only
    void set_value(double v) { _value = v; }
private:
    double _value;
};

class RandomSensor : public Sensor
{
public:
    RandomSensor(double low, double high)
    : _distribution(std::uniform_real_distribution<double>(low, high)),
      _engine(std::random_device()()) {}

    virtual double get_value() override
    {
        return _distribution(_engine);
    }

private:
    std::uniform_real_distribution<double> _distribution;
    std::default_random_engine _engine;
};

class Averager
{
public:
    Averager(Sensor* left, Sensor* right)
    : _left(left), _right(right) {}

    double get_average() { return (_left->get_value() + _right->get_value()) / 2; }

private:
    Sensor* _left;
    Sensor* _right;
};


int main(int argc, char** argv)
{
    std::string how = argv[1];

    MockSensor left(2.0);
    MockSensor right(7.5);

    Averager avg(&left, &right);

    double v = avg.get_average();
    std::println("Average value: {}", v);

    return 0;
}
  • Commandline args “mock”/”random”

  • Explain why dynamic allocation needed (object lifetime, debugging etc.)

  • Missing deletevalgrind

  • Return before delete, or exeption ⟶ valgrind

  • Comment out for a moment, and show std::shared_ptr<Sensor> and derived types std::shared_ptr

  • Fix using std::shared_ptr<>, including Averager ctor

And std::unique_ptr<>?

  • Comment out again

  • Show std::unique_ptr<> like before, including move(-only) semantics

  • Consequences of using std::unique_ptr<>?