简体   繁体   English

为什么我不能使用static_cast <int&>将整数引用参数传递给C ++中的函数?

[英]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 an int 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.

相关问题 C ++:不能从double *到int *的static_cast - C++: can't static_cast from double* to int* 为什么我可以static_cast void *到int *但不能到int *&? - Why can I static_cast void* to int* but not to int*&? 为什么C ++不允许进行static_cast <function_type_of_f> (F)? - Why doesn't C++ allow to static_cast<function_type_of_f>(f)? C ++:如何避免static_cast? - c++: how can I avoid the static_cast? 为什么我应该在C ++中使用&#39;static_cast&#39;进行数值转换? - Why should I use 'static_cast' for numeric casts in C++? 为什么我可以对static * cast使用void *但不能对char *使用 - Why can I use static_cast With void* but not With char* 为什么我不能 static_cast char* 到 std::byte*? - Why I can't static_cast char* to std::byte*? C样式转换和C ++ static_cast引用指针 - C Style cast and C++ static_cast to reference of pointer 为什么我不能将 void* static_cast 转换为函数指针? - Why can't I static_cast a void* to a pointer-to-function? 在Effective C ++ Item 3中,为什么要使用static_cast <const TextBlock&> (* this)而不是static_cast <const TextBlock> (*这个)? - In Effective C++ Item 3,why use static_cast<const TextBlock&>(*this) instead of static_cast<const TextBlock>(*this)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM