[英]Why can't I use static_cast<int&> to pass an integer reference parameter to a function in C++?
I have an enum parameter in a C++ program that I need to obtain using a function that returns the value through a parameter. 我在C ++程序中有一个枚举参数,我需要使用一个通过参数返回值的函数来获取该参数。 I started by declaring it as an int but at code review was asked to type it as the enum (ControlSource).
我首先将其声明为int,但在代码审查时要求将其键入为枚举(ControlSource)。 I did this but it breaks the Get() function - I noticed that a C-style cast to int& resolves the problem, but when I first tried to fix it with a static_cast<> it didn't compile.
我做了这个,但它打破了Get()函数 - 我注意到一个C样式转换为int并解决了问题,但是当我第一次尝试使用static_cast <>修复它时,它没有编译。
Why is this, and why is it that when eTimeSource was an int no casting is required at all to pass the integer by reference? 为什么会这样,为什么当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?
When you convert a variable to a value of a different type, you obtain a temporary value, which cannot be bound to a non-constant reference: It makes no sense to modify the temporary. 当您将变量转换为不同类型的值时,您将获得一个临时值,该值不能绑定到非常量引用:修改临时值是没有意义的。
If you just need to read the value, a constant reference should be fine: 如果您只需要读取值,则常量引用应该没问题:
static_cast<int const &>(eTimeSource)
But you might as well just create an actual value, rather than a reference: 但您也可以创建一个实际值,而不是引用:
static_cast<int>(eTimeSource)
static_cast<int&>((eTimeSource))); //This doesn't work.
Right, it doesn't work, because eTimeSource
is not an int
so you can't bind a int&
to it. 是的,它不起作用,因为
eTimeSource
不是一个int
所以你不能绑定一个int&
它。
(int&)((eTimeSource))); //This does work.
Wrong, that doesn't work either, it just appears to. 错了,这也不起作用,它似乎只是。 The C-style cast lies to the compiler and says "just make it this type, even if that's not legal".
C风格的演员阵容归于编译器,并说“只要把它变成这种类型,即使这不合法”。 Just because something compiles doesn't mean it works.
仅仅因为编译的东西并不意味着它有效。
why is it that when
eTimeSource
was anint
no casting is required at all to pass the integer by reference?为什么当
eTimeSource
是一个int
根本不需要转换来通过引用传递整数?
Because you can bind an int&
to an int
but not to a different type, and eTimeSource
is a different type. 因为您可以将
int&
int
绑定到不同的类型,而eTimeSource
是不同的类型。 An int&
is a reference to an int
. 一个
int&
是一个参考int
。 If you could bind it to a different type it wouldn't refer to an int
, would it? 如果你可以将它绑定到不同的类型,它就不会引用
int
,不是吗?
If the code reviewer said to change the variable to an enumeration type they probably also meant for you to change the function parameter to take a ControlSource&
如果代码审阅者说要将变量更改为枚举类型,那么他们可能还意味着您需要更改函数参数以获取
ControlSource&
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.