我看过以下代码:
template <class T>
class Type {
public:
Type() {}
T& operator=(const T& rhs) {value() = rhs; return value();}
T& value() {return m_value;}
T value() const {return m_value;}
private:
T m_value;
};
Run Code Online (Sandbox Code Playgroud)
为什么编译器不抱怨
T& value() {return m_value;}
T value() const {return m_value;}
Run Code Online (Sandbox Code Playgroud)
以及如何知道调用哪一个?
使用普通枚举,我可以使用以下代码访问 Q_ENUMS 属性和具体的枚举的字符表示:
// in .h
class EnumClass : public QObject
{
Q_OBJECT
public:
enum MyEnumType { TypeA, TypeB };
Q_ENUMS(MyEnumType)
private:
MyEnumType m_type;
};
// in .cpp
m_type = TypeA;
...
const QMetaObject &mo = EnumClass::staticMetaObject;
int index = mo.indexOfEnumerator("MyEnumType");
QMetaEnum metaEnum = mo.enumerator(index);
QString enumString = metaEnum.valueToKey(m_type); // contains "TypeA"
Run Code Online (Sandbox Code Playgroud)
如果我想将 c++11 功能用于强类型枚举,例如
enum class MyEnumType { TypeA, TypeB };
Run Code Online (Sandbox Code Playgroud)
访问元信息不再起作用。我想,Qt 不再将其识别为枚举。
是否有任何解决方案可以在使用强类型枚举时访问枚举的字符表示?