运算符重载的奇怪之处(C ++)

时间:2012-05-03 05:24:46

标签: c++ operator-overloading

###MyClass.h###

    #ifndef _MyClass
    #define _MyClass
    #include <string>
    using namespace std;


    class MyClass
    {
    public:
        MyClass(const string name, const string text);
        void display(ostream & out) const;

        MyClass & operator = (const MyClass & m);
        int compare(const MyClass & right) const;

    private:
        string _name;
        string _text;
    };

    bool operator < (const MyClass & left, const MyClass & right);
    ostream & operator << (ostream & out, const MyClass & mc);
    #endif

###Node.h###

    #include <string>
    #include "MyClass.h"
    using namespace std;
    typedef MyClass * DataType; 

    class Node
    {
    private:
        DataType item; // data
        Node * lchild; // left child pointer
        Node * rchild; // right child pointer

    public:
        Node(DataType Item);
        DataType getItem() const;
        void setItem(const DataType  & data);
        Node* getLChild() const;
        void setLChild(Node * p);
        Node* getRChild() const;
        void setRChild(Node * p);
        virtual ~Node();
    };

###BST.h###

    #include "Node.h"
    using namespace std;

    class BST
    {
    private:
            Node * root; 
            bool Search(const DataType item, Node * r) const; 
            void Insert (DataType item, Node * ptr);
            void Destructor(const Node * r);

    public:
        BST();
        bool IsEmpty() const;
        void Insert(const DataType item);
        bool Search(const DataType item) const;
        virtual ~BST();
    };

###MyClass.cpp###

    #include <iostream>
    #include "MyClass.h"
    using namespace std;

    MyClass::MyClass(const string name, const string text)
    {
        _name = name;
        _text = text;
    }

    void MyClass::display(ostream & out) const
    {
        out << "Name: " << _name << endl;
        out << "Text: " << _text << endl;
    }

    MyClass & MyClass::operator = (const MyClass & m)  
    {  
        if (this == & m)
            return *this;

        _name = m._name;
        _text = m._text;

        return *this; 
    }

    int MyClass::compare(const MyClass & right) const
    {
        return _name.compare(right._name);
    }

    bool operator < (const MyClass & left, const MyClass & right)  
    {  
        return left.compare(right) > 0;
    }

    ostream & operator << (ostream & out, const MyClass & mc)
    {
        mc.display(out);
        return out;
    }

###Node.cpp###

    #include "Node.h"

    Node::Node(DataType Item):item(Item)
    {
        lchild = 0;
        rchild = 0;
    }

    DataType Node::getItem() const
    {
        DataType anItem = item; 
        return anItem;
    }

    void Node::setItem( const DataType & data)
    {
        item = data;
    }

    Node* Node::getLChild() const
    {
        Node * p = lchild;
        return p;
    }

    void Node::setLChild(Node * p)
    {
        lchild = p;
    }

    Node* Node::getRChild() const
    {
        Node * p = rchild;
        return p;
    }

    void Node::setRChild(Node * p)
    {
        rchild = p;
    }

    Node::~Node()
    {
    }

###BST.cpp###

    #include <iostream>
    #include "BST.h"
    using namespace std;

    bool BST::Search(const DataType item) const
    {
        return Search(item, root);
    }

    bool BST::Search(const DataType item, Node * r) const
    {
        if(r != 0)
        {
            if (item == r->getItem())
                return true;
            else
            {
                if (item < r->getItem())
                    return Search(item, r->getLChild());
                else
                    return Search(item, r->getRChild());
            }
        }
        else
            return false;
    }

    BST::BST()
    {
        root = 0;
    }

    bool BST::IsEmpty() const
    {
        return (root == 0);
    }

    void BST::Insert(const DataType item)
    {
        if(root == 0)
            root = new Node(item);
        else
            Insert(item, root);
    }

    void BST::Insert(DataType item, Node * ptr)
    {
        if (item < ptr->getItem())
        {
            if (ptr->getLChild() == 0)
                ptr->setLChild(new Node(item));
            else 
                Insert(item, ptr->getLChild());
        }
        else 
        {
            if (ptr->getRChild() == 0)
                ptr->setRChild(new Node(item));
            else 
                Insert(item, ptr->getRChild());
        }
    } 

    void BST::Destructor(const Node * r)
    {
        if(r!=0)
        {
            Destructor( r->getLChild());
            Destructor( r->getRChild());
            delete r;
        }
    }

    BST::~BST()
    {
        Destructor(root);
    }

###main.cpp###

    #include <iostream>
    #include "MyClass.h"
    #include "BST.h"
    using namespace std;

    void main()
    {
        MyClass * mc1 = new MyClass("Tree","This is a tree");
        MyClass * mc2 = new MyClass("Book","This is a book");
            MyClass * mc3 = new MyClass("Zoo","This is a zoo");

        BST * tree = new BST();
        tree->Insert(mc1);
        tree->Insert(mc2);
        tree->Insert(mc3);

        cout << boolalpha << ("Book" < "Tree") << endl;
        cout << (mc2 < mc1) << endl;

        cout << (tree->Search(new MyClass("Book",""))) << endl;
    }
  

结果为true false false

     
      
  1. 我不知道我的运算符重载有什么问题? (mc2应该   小于mc1)
  2.   
  3. 我不确定这是否适合在BST中搜索“MyClass”节点?   结果是“找不到”....我将其追溯到“BST.cpp”,   并发现问题也发生在“if(item&lt; r-&gt; getItem())”
  4.         

    任何人都可以帮助我或给我一个提示....非常感谢你!

1 个答案:

答案 0 :(得分:4)

这里只是比较指针,即内存地址:

cout << (mc2 < mc1) << endl;

要比较对象,您需要取消引用指针:

cout << ((*mc2) < (*mc1)) << endl;

在您的代码段中,mc1mc2等没有理由成为指针,因此您可以通过直接在堆栈上创建对象来避免此问题:

MyClass mc1("Tree","This is a tree");

等等。我甚至会更进一步说,如果您确实需要并且有充分的理由不在堆栈上自动分配,那么您应该只使用new动态分配对象。如果你真的必须使用动态分配的指针,请查看C++ smart pointers