From f0dff763f372d4ea6fb12f69d023c8dddfe4957f Mon Sep 17 00:00:00 2001 From: NilsForssen Date: Mon, 9 Oct 2023 13:07:40 +0200 Subject: [PATCH] Added destructors and memory management --- src/expression.cc | 31 ++++++++++++++++++++++++++++++- src/expression.h | 20 ++++++++++---------- src/operators.cc | 6 ++++++ src/operators.h | 3 +-- src/uppgift13.cc | 2 +- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/expression.cc b/src/expression.cc index ed2cd5f..ff1a8f0 100644 --- a/src/expression.cc +++ b/src/expression.cc @@ -36,7 +36,7 @@ Expression::Expression(const std::string & expression) { throw std::logic_error("Missing operands"); } - + if (token == "+") { new_node = new Addition{*lhs, *rhs}; @@ -80,6 +80,17 @@ Expression::Expression(const std::string & expression) expression_root = stack.top(); } +Expression::Expression(Expression && other) +: expression_root{nullptr} +{ + *this = std::move(other); +} + +Expression::~Expression() +{ + delete expression_root; +} + std::string Expression::to_infix_string() const { return expression_root->get_infix(); @@ -98,4 +109,22 @@ std::string Expression::to_prefix_string() const double Expression::evaluate() const { return expression_root->get_value(); +} + +void Expression::unlink() +{ + expression_root = nullptr; +} + +Expression & Expression::operator=(Expression && other) +{ + if (this == &other) + { + // Do nothing, return + return *this; + } + + expression_root = other.expression_root; + other.unlink(); + return *this; } \ No newline at end of file diff --git a/src/expression.h b/src/expression.h index 7ae5a97..4505b7a 100644 --- a/src/expression.h +++ b/src/expression.h @@ -3,21 +3,21 @@ #include #include +#include //Behövs? +#include //Behövs? +#include #include "postfix.h" #include "token.h" #include "operators.h" #include "operands.h" -#include // TA bort -#include -#include -#include - class Expression { public: Expression(const std::string & expression); + Expression(Expression && other); // Move constructor + ~Expression(); Expression(Expression const&) = delete; Expression& operator=(Expression const&) = delete; @@ -25,16 +25,16 @@ public: std::string to_infix_string() const; std::string to_postfix_string() const; std::string to_prefix_string() const; - + + Expression & operator=(Expression && other); // Move operator + double evaluate() const; + void unlink(); + protected: private: Node *expression_root; }; -class Result -{ -}; - #endif \ No newline at end of file diff --git a/src/operators.cc b/src/operators.cc index 5cdf094..f81a23e 100644 --- a/src/operators.cc +++ b/src/operators.cc @@ -4,6 +4,12 @@ Operator::Operator(Node * l, Node * r, std::string sym) : rhs{r}, lhs{l}, symbol{sym} {} +Operator::~Operator() +{ + delete rhs; + delete lhs; +} + std::string Operator::get_symbol() const { return symbol; diff --git a/src/operators.h b/src/operators.h index 5023a39..d067fe9 100644 --- a/src/operators.h +++ b/src/operators.h @@ -16,8 +16,7 @@ public: Operator(Operator const &) = delete; Operator & operator=(Operator const &) = delete; - virtual ~Operator() = default; - + ~Operator(); std::string get_symbol() const; std::string get_postfix() const override; std::string get_infix() const override; diff --git a/src/uppgift13.cc b/src/uppgift13.cc index d5252f5..129080d 100644 --- a/src/uppgift13.cc +++ b/src/uppgift13.cc @@ -44,7 +44,7 @@ int main() } else { - Expression e{line}; + e = line; } } return 0; -- 2.30.2