2016-09-24 82 views
2

如何從父類中的函數調用類中的重載關係運算符,該父類將常量引用作爲參數傳遞給基類?下面的代碼演示我想該怎麼做:如何從父類中引用父類的函數作爲參數調用子類的重載運算符?

class Object 
{ 
public: 
    virtual ~Object(void); 
    virtual int compare(Object const& obj) const; 
}; 

int Object::compare(Object const & obj) const { 
    if(this == &obj) 
    { 
     return 0; 
    } 
    else if(this < &obj) 
    { 
     return -1; 
    } else{ 
     return 1; 
    } 
} 

class Integer: public Object 
{ 
private: 
    int myInt; 
public: 
    Integer(int i); 
    bool operator==(const Integer& integer); 
}; 

bool Integer::operator==(Integer const &integer) { 
    if(myInt == integer.myInt) 
    { 
     return true; 
    } 
    return false; 
} 

我如何在基類的比較函數來調用子類==操作符,牢記我有其他的子類,以及?

我試過dynamic_cast <>但由於某種原因,它不會工作。

+0

注意你只是比較地址不是對象在基類中。 – xinaiz

回答

0

您可以添加另一個虛擬方法isEqualInteger::operator==連接。

唯一的要求是保留Integer中的Object::isEqual簽名。您還應該記住,您的Object::compare方法可能(意外)在不同類型的對象上調用。

class Object 
{ 
protected: 
    virtual bool isEqual(Object const& obj) const 
     { return this == &obj; } 
public: 
    virtual ~Object(void); 
    virtual int compare(Object const& obj) const; 
}; 

int Object::compare(Object const & obj) const { 
    if(isEqual(obj)) 
    { 
     return 0; 
    } 
    else if(this < &obj) 
    { 
     return -1; 
    } else{ 
     return 1; 
    } 
} 

class Integer: public Object 
{ 
private: 
    int myInt; 
protected: 
    virtual bool isEqual(Object const& obj) const 
     { if (!dynamic_cast<const Integer*>(&obj)) 
      return false; 
     return *this == (const Integer&) obj; 
     } 
public: 
    Integer(int i); 
    bool operator==(const Integer& integer); 
}; 
+0

謝謝,這工作。現在,如果我想爲另一個子類創建相同的功能,只需在另一個子類中實現isEqual? – Keagansed

+0

@Keagansed是的,它應該工作。你也可以重寫子類中的方法比較來直接調用'operator =='。 'isEqual'返回'false'時的默認指針比較可能是錯誤的,或者至少很難調試(在非調試模式下不可重現)。 – Franck

+0

非常感謝您的幫助。我所做的幾乎是你說的..我重寫比較在子類中測試是否可以進行動態轉換,如果它不是我返回的Object :: compare(obj),它將地址簡單地作爲I這樣做至少有一個默認的操作,如果可能的話,我直接調用operator ==,就像你在isEqual中所做的那樣。我想我可以upvote你的答案,但我沒有足夠的聲譽這樣做..再次感謝您的幫助。 – Keagansed

0

我會做:

#include <iostream> 

class Object 
{ 
public: 
    virtual ~Object(void) {}; 
    int compare(Object const& obj) const; 
    virtual bool operator==(Object const& integer) const = 0; 
    virtual bool operator<(Object const& integer) const = 0; 
    virtual bool operator>(Object const& integer) const = 0; 
}; 

int Object::compare(Object const& obj) const 
{ 
    if(*this == obj) 
     return 0; 
    else if(*this < obj) 
     return -1; 
    else return 1; 
} 

class Integer: public Object 
{ 
private: 
    int myInt; 
public: 
    Integer(int i) : myInt(i) { }; 
    virtual bool operator==(Object const& integer) const override; 
    virtual bool operator<(Object const& integer) const override; 
    virtual bool operator>(Object const& integer) const override; 
}; 

bool Integer::operator==(Object const& integer) const 
{ 
    return myInt == dynamic_cast<Integer const&>(integer).myInt; 
} 

bool Integer::operator<(Object const& integer) const 
{ 
    return myInt < dynamic_cast<Integer const&>(integer).myInt; 
} 

bool Integer::operator>(Object const& integer) const 
{ 
    return myInt > dynamic_cast<Integer const&>(integer).myInt; 
} 
int main() 
{ 
    Integer a(2), b(2), c(3); 
    std::cout << a.compare(b) << std::endl; 
    std::cout << b.compare(c) << std::endl; 
    std::cout << c.compare(a) << std::endl; 
} 

但在現實中,你應該只提供虛擬compare功能繼承類,像這樣:

class Object 
{ 
public: 
    virtual ~Object(void) {}; 
    virtual int compare(Object const& obj) const = 0; 
}; 

class Integer: public Object 
{ 
private: 
    int myInt; 
public: 
    Integer(int i) : myInt(i) { }; 
    virtual int compare(Object const& object) const override; 
    bool operator==(Integer const& integer) const; 
    bool operator<(Integer const& integer) const; 
    bool operator>(Integer const& integer) const; 
}; 

int Integer::compare(Object const& object) const 
{ 
    Integer const& ref = dynamic_cast<Integer const&>(object); 
    if(ref == *this) 
     return 0; 
    else if(ref > *this) 
     return 1; 
    else return -1; 
} 

bool Integer::operator==(Integer const& integer) const 
{ 
    return myInt == integer.myInt; 
} 

bool Integer::operator<(Integer const& integer) const 
{ 
    return myInt > integer.myInt; 
} 

bool Integer::operator>(Integer const& integer) const 
{ 
    return myInt < integer.myInt; 
} 
+0

我得到一個錯誤,說不能比較結構,如果我使用* this == obj – Keagansed