[英]Operator overloading of enum class in class
我在嵌套類中使用私有枚舉類,並想實現運算符! 對於我的枚舉類。
我知道該怎么做。 但是,當我嘗試在嵌套類中霸占枚舉類的運算符時,編譯器會將我的運算符視為類的運算符,而不是枚舉類。
class test{
private:
enum class Loc : bool{
fwrd = true,
bkrd = false
};
Loc Loc::operator!(){ //error msg 1.
return Loc(!bool(*this));
}
Loc operator!(){
return something; //this treated as test's operator
}
Loc doSomething(Loc loc){
return !loc; //error msg 2.
}
}
enum class Other : bool{
fwrd = true,
bkrd = false
};
Other operator!(Other o){ //this works
return Other(!bool(*this));
}
錯誤訊息
您可以使用friend
功能:
class test
{
private:
enum class Loc : bool{
fwrd = true,
bkrd = false
};
friend Loc operator!(Loc loc){
return Loc(!bool(loc));
}
Loc doSomething(Loc loc){
return !loc;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.