From 828a41dd72f64966f832652fe968f73601aea8e3 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Tue, 25 May 2021 15:56:40 +0200 Subject: [PATCH] Library 3 bug fix: when size==0 and a BinaryTree[Lnk/Vec] is casted to BinaryTree, and operator== is called, it crashed --- librerie/exercise3/binarytree/binarytree.cpp | 10 +++++++--- librerie/exercise3/binarytree/lnk/binarytreelnk.cpp | 13 +++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index 9216be4..028f727 100755 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -61,9 +61,13 @@ const Data& BinaryTree::Node::Element() const{ /* ----- end of struct Node ----- */ template -bool BinaryTree::operator==(const BinaryTree& toCompare) const noexcept{ - if(size!=toCompare.size) return false; - return(Root() == toCompare.Root()); +bool BinaryTree::operator==(const BinaryTree& tree) const noexcept{ + if(size == tree.size){ + if(size == 0) return true; + else return (Root() == tree.Root()); + }else{ + return false; + } } template diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp index 5b49da9..eca6ed9 100755 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp @@ -140,14 +140,15 @@ BinaryTreeLnk& BinaryTreeLnk::operator=(BinaryTreeLnk&& tree) return *this; } +/* +** operator== and operator!= can be removed from BinaryTreeLnk since they are +** inherited from BinaryTree. They're here just for clarity and because they're +** in the template. +** Maybe you can make them more optimized here. +*/ template bool BinaryTreeLnk::operator==(const BinaryTreeLnk& tree) const noexcept{ - if(size == tree.size){ - if(size == 0) return true; - else return (Root() == tree.Root()); - }else{ - return false; - } + return (BinaryTree::operator==(tree)); } template