为什么C++不能将
例如:http://ideone.com/Ns1O2d
enum MyEnum : int
识别为协变至int
?例如:http://ideone.com/Ns1O2d
#include <iostream>
enum FooType : int
{
Crazy = 0,
Cool
};
enum BarType : int
{
Hello = Cool + 1,
World
};
class Foo
{
public:
Foo(void)
{
}
~Foo(void)
{
}
virtual int getType(void)
{
return Crazy;
}
};
class Bar : public Foo
{
public:
Bar(void)
{
}
~Bar(void)
{
}
virtual BarType getType(void)
{
return Hello;
}
};
int main(int argc, char* argv[])
{
Bar f = Bar();
std::cout << f.getType() << std::endl;
return 0;
}
编译错误:
prog.cpp:43:18: error: conflicting return type specified for 'virtual BarType Bar::getType()'
prog.cpp:26:14: error: overriding 'virtual int Foo::getType()'
Bar::getType()
的返回类型从BarType
改为int
(而不做其他任何更改),就可以达到所需的效果,对吗? - jogojapanFooType
和BarType
是不同的类型,如果你想将你的BarType
强制转换为int
,你可以return (int)Crazy;
。 - masaers