[英]C++11 Overloading operator for in class placed enum class
this is my first try to use enum classes for my projects, but I have the problem that I can't compile my code if the enum class is placed inside of another class. 这是我第一次尝试为我的项目使用枚举类,但我遇到的问题是,如果将枚举类放在另一个类中,我就无法编译代码。 I try to define the operator overloading like my example and I try to do it outside, too.
我尝试像我的例子一样定义运算符重载,我也试着在外面做。 All works fine if I place the enum class outside the class.
如果我将enum类放在课堂外,一切正常。 Whats wrong?
怎么了? How to overloading the operator if I what to use it placed in an class?
如果我把它放在一个类中,如何重载操作符?
#include <cstdint>
namespace MyNamespace
{
class MyClass
{
public:
enum class MyEnum_t
{
VALUE_0 = 0x0,
VALUE_1 = 0x1,
VALUE_2 = 0x2,
VALUE_3 = 0x4,
VALUE_4 = 0x8
};
inline MyEnum_t &operator|(MyEnum_t lhs, MyEnum_t rhs)
{
return static_cast<MyEnum_t>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
}
}
int main()
{
MyNamespace::MyClass::MyEnum_t test = MyNamespace::MyClass::MyEnum_t::VALUE_0;
test = MyNamespace::MyClass:MyEnum_t::VALUE_1 | MyNamespace::MyClass::MyEnum_t::VALUE_2;
return 0;
}
The enum class can be inside another class, but the operator definition must be at namespace scope. 枚举类可以在另一个类中,但运算符定义必须在命名空间范围内。
Also note that the operator is computing a new value and as such, it cannot return a reference, as there would be nothing to which the reference could bind. 另请注意,运算符正在计算新值,因此,它无法返回引用,因为引用无法绑定任何内容。 It should return by value instead.
它应该按值返回。
In total: 总共:
namespace MyNamespace
{
class MyClass
{
public:
enum class MyEnum_t
{
VALUE_0 = 0x0,
VALUE_1 = 0x1,
VALUE_2 = 0x2,
VALUE_3 = 0x4,
VALUE_4 = 0x8
};
};
inline MyClass::MyEnum_t operator|(MyClass::MyEnum_t lhs, MyClass::MyEnum_t rhs)
{
return static_cast<MyEnum_t>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
}
}
I would write it as: 我会把它写成:
class MyClass
{
public:
enum class MyEnum_t
{
VALUE_0 = 0x0,
VALUE_1 = 0x1,
VALUE_2 = 0x2,
VALUE_3 = 0x4,
VALUE_4 = 0x8,
};
friend MyEnum_t operator|(MyEnum_t lhs, MyEnum_t rhs)
{
using UT = std::underlying_type<MyEnum_t>::type;
return static_cast<MyEnum_t>(static_cast<UT>(lhs) | static_cast<UT>(rhs));
}
};
This way, it no longer matters whether or not MyClass
is in a namespace
, and the correct underlying_type
is used to perform the bitwise math. 这样,
MyClass
是否在namespace
就不再重要了,并且正确的underlying_type
用于执行按位数学运算。
Fixed code for the operator: 修复了运营商的代码:
inline MyClass::MyEnum_t operator|(MyClass::MyEnum_t lhs, MyClass::MyEnum_t rhs)
{
return static_cast<MyClass::MyEnum_t>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
}
As you wrote your following main() code line: 在编写以下main()代码行时:
test = MyNamespace::MyClass:MyEnum_t::VALUE_1 | test = MyNamespace :: MyClass:MyEnum_t :: VALUE_1 | MyNamespace::MyClass::MyEnum_t::VALUE_2;
myNameSpace对象:: MyClass的:: MyEnum_t :: VALUE_2;
It will expect namespace operator, so the operator should be placed outside the MyClass (but within the MyNamespace): 它会期望命名空间操作符,因此操作符应放在MyClass之外(但在MyNamespace中):
MyClass::MyEnum_t operator|(MyClass::MyEnum_t lhs, MyClass::MyEnum_t rhs)
{
return static_cast<MyClass::MyEnum_t>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
}
also, as stated in above answer you can not return a temporary memory (as reference), so either return by-value or define static variable in the operator and return it. 另外,如上面的答案中所述,您不能返回临时内存(作为引用),因此要么返回值,要么在运算符中定义静态变量并返回它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.