![](/img/trans.png)
[英]How can I overload operator [] for a class containing an array of an abstract class in c++?
[英]How can I overload an operator for an abstract class?
我是C ++的新手,我的教授沒有像上課時那樣詳細介紹運算符重載。 我試圖實現一種比較對象(使用>或<)的方法,這些對象都繼承了一個抽象類,但是在語法/邏輯方面遇到了麻煩。
我試圖讓它成為父類的成員,但是我不知道如何從基類內部調用純虛函數。 然后我嘗試使用模板,但這只是讓我頭疼(我的教授也沒有對它們進行太深入的研究)。
我知道我在運算符函數中完全失敗了(使用適當語法的任何幫助將不勝感激)。
#include <iostream>
enum cType { point, maxima, inflection };
class CONSTRAINT {
public:
//coordinates
int x, y;
//gets what type of constraint the object is
virtual cType getType() = 0; //pure virtual
//I'm sure this syntax is horrendous and completely wrong.
//I was just trying to emulate what I found online :(
bool operator > (const CONSTRAINT &rhs) {
//If the constraints have the same type, compare by their x-value
if (getType() == rhs.getType())
return (x > rhs.x);
//Otherwise, it should be point > maxima > inflection
else
return (getType() > rhs.getType());
}
};
class POINT : public CONSTRAINT {
public:
virtual cType getType() { return point; }
};
class MAXIMA : public CONSTRAINT {
public:
virtual cType getType() { return maxima; }
};
//I have another inflection class that follows the pattern
int main() {
POINT point1, point2;
point1.x = 3;
point2.x = 5;
MAXIMA maxima;
maxima.x = 4;
std::cout << (point1 > point2);
std::cout << (point2 > point1);
std::cout << (maxima > point2);
std::cout << (point1 > maxima );
return 0;
}
我希望:如果程序可以編譯,則為0110。
相反,我得到以下錯誤:
“對象的類型限定符與成員函數“ CONSTRAINT :: getType”不兼容”
“'cType CONSTRAINT :: getType(void)':無法將'this'指針從'const CONSTRAINT'轉換為'CONSTRAINT&'
謝謝。
在
bool operator > (const CONSTRAINT &rhs)
rhs
是const
。 無法在此方法內更改。 但...
virtual cType getType() = 0; //pure virtual
不是const
方法。 這意味着該方法可以更改rhs
,因此編譯器拒絕允許調用它。
解決方案:聲明方法const
virtual cType getType() const = 0; //pure virtual
現在,承諾編譯器將不允許調用該函數來更改rhs
。 如果getType
的實現嘗試更改對其調用的對象,則編譯器還將強制執行此操作,並拒絕編譯程序。
旁注:
一旦方法被聲明為virtual
,所有覆蓋也將為virtual
。
如果方法應該重寫但不是由於不匹配而引起的, override
關鍵字將捕獲錯誤。 將const
添加到基類方法而不是派生類方法是一個很好的例子。
由於該代碼似乎利用了運行時多態性,因此,如果您有一天希望通過指向基類的指針delete
派生類,則您可能希望基類中的虛擬析構函數可以確保銷毀正確的類。
包裝所有這些:
#include <iostream>
enum cType { point, maxima, inflection };
class CONSTRAINT {
public:
//coordinates
int x, y;
virtual ~CONSTRAINT() = default;
// ^ added
//gets what type of constraint the object is
virtual cType getType() const = 0; //pure virtual
// ^ added
//I'm sure this syntax is horrendous and completely wrong.
//I was just trying to emulate what I found online :(
bool operator > (const CONSTRAINT &rhs) {
//If the constraints have the same type, compare by their x-value
if (getType() == rhs.getType())
return (x > rhs.x);
//Otherwise, it should be point > maxima > inflection
else
return (getType() > rhs.getType());
}
};
class POINT : public CONSTRAINT {
public:
cType getType() const override { return point; }
// ^ added ^ added
};
class MAXIMA : public CONSTRAINT {
public:
cType getType() const override { return maxima; }
// ^ added ^ added
};
//I have another inflection class that follows the pattern
int main() {
POINT point1, point2;
point1.x = 3;
point2.x = 5;
MAXIMA maxima;
maxima.x = 4;
std::cout << std::boolalpha // < added. prints true and false instead of 1 and 0
<< (point1 > point2) << '\n'
<< (point2 > point1) << '\n'
<< (maxima > point2) << '\n'
<< (point1 > maxima);
// took advantage of chaining and added newlines to the output for clarity
return 0;
}
最后一點說明:通常建議將operator<
實現為Free Function 。 有關此內容的更多信息以及有關運算符重載的更多其他知識,請參閱運算符重載的基本規則和慣用法是什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.