online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <memory> #include <iostream> #include <utility> class BinarySearchTree { public: BinarySearchTree(); BinarySearchTree(int value); BinarySearchTree(const BinarySearchTree& other_tree); BinarySearchTree(BinarySearchTree&& other_tree); BinarySearchTree& operator=(const BinarySearchTree& other_tree); BinarySearchTree& operator=(BinarySearchTree&& other_tree); ~BinarySearchTree() = default; void clear(); inline int size() const { return tree_size; } inline bool empty() const { return tree_size == 0; } private: struct Node { int val; std::unique_ptr<Node> left = nullptr; std::unique_ptr<Node> right = nullptr; Node(const int value) : val{value}, left{nullptr}, right{nullptr} {} }; std::unique_ptr<Node> root; int tree_size; void deep_copy_tree(std::unique_ptr<Node>& dest_node, const std::unique_ptr<Node>& source_node); }; BinarySearchTree::BinarySearchTree() : root{nullptr}, tree_size{0} { std::cout << "BinarySearchTree() constructor\n"; } BinarySearchTree::BinarySearchTree(int value) : root{std::make_unique<Node>(value)}, tree_size{1} { std::cout << "BinarySearchTree(int value) constructor\n"; } BinarySearchTree::BinarySearchTree(const BinarySearchTree& other_tree) : root{nullptr}, tree_size{0} { std::cout << "Copy constructor\n"; if (other_tree.tree_size == 0) return; tree_size = other_tree.tree_size; deep_copy_tree(root, other_tree.root); } BinarySearchTree::BinarySearchTree(BinarySearchTree&& other_tree) : root(std::exchange(other_tree.root, nullptr)), tree_size(std::exchange(other_tree.tree_size, 0)) { std::cout << "Move constructor\n"; } BinarySearchTree& BinarySearchTree::operator=(const BinarySearchTree& other_tree) { std::cout << "Copy assignment operator\n"; clear(); tree_size = other_tree.tree_size; deep_copy_tree(root, other_tree.root); return *this; } BinarySearchTree& BinarySearchTree::operator=(BinarySearchTree&& other_tree) { std::cout << "Move assignment operator\n"; clear(); tree_size = other_tree.tree_size; other_tree.tree_size = 0; root = std::move(other_tree.root); return *this; } void BinarySearchTree::clear() { root = nullptr; tree_size = 0; } void BinarySearchTree::deep_copy_tree(std::unique_ptr<Node>& dest_node, const std::unique_ptr<Node>& source_node) { if (!source_node) return; dest_node = std::make_unique<Node>(source_node->val); deep_copy_tree(dest_node->left, source_node->left); deep_copy_tree(dest_node->right, source_node->right); } int main() { BinarySearchTree myBST1(5); BinarySearchTree myBST2 = myBST1; // copy constructor BinarySearchTree myBST3(4); myBST3 = myBST1; // copy assignment std::cout << "myBST3.empty() before move: " << myBST3.empty() << '\n'; BinarySearchTree myBST4(std::move(myBST3)); // move constructor std::cout << "myBST3.empty() after move: " << myBST3.empty() << '\n'; std::cout << "myBST4.empty() before move assignment: " << myBST4.empty() << '\n'; myBST2 = std::move(myBST4); // move assignment std::cout << "myBST4.empty() after move assignment: " << myBST4.empty() << '\n'; return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue