我在一个C++程序中有一个枚举参数,我需要使用一个通过参数返回值的函数来获取它。一开始我把它声明为int类型,但是在代码审查时被要求将其类型更改为枚举(ControlSource)。我这样做了,但它破坏了Get()函数——我注意到使用C风格的int&转换可以解决问题,但当我尝试使用static_cast<>修复它时,它无法编译。
为什么会这样呢?而当eTimeSource是int类型时,根本不需要进行任何转换就可以通过引用传递整数?
为什么会这样呢?而当eTimeSource是int类型时,根本不需要进行任何转换就可以通过引用传递整数?
//GetCuePropertyValue signature is (int cueId, int propertyId, int& value);
ControlSource eTimeSource = ControlSource::NoSource;
pPlayback->GetCuePropertyValue(programmerIds.cueId, DEF_PLAYBACKCUEPROPERTY_DELAY_SOURCE, static_cast<int&>(eTimeSource)); //This doesn't work.
pPlayback->GetCuePropertyValue(programmerIds.cueId, DEF_PLAYBACKCUEPROPERTY_DELAY_SOURCE, (int&)(eTimeSource)); //This does work.
int nTimeSource = 0;
pPlayback->GetCuePropertyValue(blah, blah, nTimeSource); //Works, but no (int&) needed... why?
reinterpret_cast
与C语言风格的“大锤式”转换也不同。如果您需要一个丑陋的hacky转换来使代码在更改类型后正常工作,请不要更改类型!这可以在int
中正常工作,但它不能在枚举中正常工作…… 对我来说答案似乎很清楚。 - Jonathan Wakely