- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 C++ 中,C 转换为类型或类型的引用有什么区别?
foo = (unsigned int) whatever;
// Is this the same as:
foo = (unsigned int&) whatever;
最佳答案
不,它甚至完全不同。
(在某些情况下,转换的确切性质取决于 whatever
是什么。我假设 whatever
的类型与 unsigned int
无关。)
引用类型的 C 转换 unsigned int &
相当于执行 reinterpret_cast
到那种类型
foo = reinterpret_cast<unsigned int &>(whatever);
根据定义等同于
foo = *reinterpret_cast<unsigned int *>(&whatever);
whatever
在这种情况下必须是左值。
换句话说,转换为引用只是类型双关的另一种方法。你很简单重新解释whatever
占用的内存作为unsigned int
目的。在一般情况下,行为是未定义的。例如,如果 sizeof whatever
小于 unsigned int
的大小,重新解释还将涉及一些甚至不属于whatever
的“狂野”内存。 .
同时,非引用 C-cast 只是一个值转换,而不是内存重新解释。对于 unsigned int
的算术转换它相当于static_cast
(但如果 whatever
是一个指针,则它等同于 reinterpret_cast
)。它读取 whatever
的值并将其转换为 unsigned int
根据语言转换规则输入(如果存在转换)。
比如这个
float f = 5;
unsigned int i = (unsigned int) f;
将转换 f
的值输入 unsigned int
,这意味着 i
将收到值 5
.同时,如果在你的平台大小unsigned int
和 float
有相同的大小,那么这个
unsigned int i = (unsigned int &) f;
实际上会重新解释 float
的内部对象表示对象 f
具有值(value) 5
作为 unsigned int
类型的对象. i
的结果值一般是不可预测的。通常它甚至不会接近 5
(在流行的实现中,您将简单地收到 float
中原始 i
值的 IEE754 表示)。
关于c++ - c-casting to type 反对 c-casting to type's reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14384681/
我的查询不工作,它总是显示相关的 0。即使针对数据确实存在。 SELECT number, MATCH(number) AGAINST('02' '01' '03' WITH QUERY EXPA
我正在尝试对字段进行全文搜索以匹配字符串的特定部分。考虑一个包含数组值的长字符串,例如 201:::1###193:::5###193:::6###202:::6。 ### 分隔数组元素,::: 分隔
我正在构建一个音乐应用程序。在我的数据库中,我有一个“艺术家”表,与“专辑”表相关,与“轨道”表相关。 我的应用程序的每个用户都可以“喜欢”(竖起大拇指)或“不喜欢”(竖起大拇指)艺术家/专辑/轨道。
我正在使用这个搜索 -> 将 (c1, c2) 与 (">dg*"在 bool 模式下匹配); 我得到了这个列表 1 dgas 2 格卡 3 dgicb 4 天 5 位数 6 dgly 7 总督 8
我想从数据库中找到 date 为“2013-04”的所有记录。 我使用: SELECT date, pastabos FROM aktai WHERE MATCH(uzsakovas) AGAINST
我正在尝试为评论实现一个 upvote/downvote 机制(类似于 reddit 上的 upvote/downvote 机制)。我有一个名为 commentReputation 的单独集合里面的文
我从事安全关键应用程序开发工作。最近,作为一名代码审阅者,我提示如下所示的编码风格,但无法提出强有力的理由反对它。那么反对这种变量冗余/重复的一个很好的论据是什么,我正在寻找可能导致问题或可能失败的测
我有一个全文索引表,其中包含数据系列。没有空格,拼写正确。如何将此查询匹配结果作为分数并按分数按 DESC 顺序显示?我总是得到空集,不知道为什么。 SELECT *, MATCH(MEDIA_TI
编辑:改写问题。 除了不好的做法,还有什么其他原因反对 HTML 中的空段落? 原创: 背景 目前,要在我们的 CMS 中添加漂亮的空格段落,您需要按两次 Enter 键。我不喜欢空洞的段落,因为它们
我正在尝试将对象转换为 Json 字符串。我看到一个 TJson::ObjectToJsonString() 似乎可以满足需求,但似乎不起作用。我在 Delphi 中有确切的代码,没有问题。因此,显然
如果我想为 Linux 开发一个类似注册表的系统,我应该避免哪些 Windows 注册表设计失败? 哪些功能是绝对必要的? 主要问题是什么(安全性、易于配置……)? 我认为 Windows 注册表不是
自从 Adobe AIR 发布以来,我想知道为什么 Java Web Start 过去没有引起更多关注,因为在我看来它似乎非常相似,但 web start 可用的时间要长得多。 主要是因为 Sun
我经常想用一个假设 free_if_heap(void *ptr)函数,因为这会让我无需关心就返回 malloc 的、静态的或堆栈对象。例如 char *foo(int arg) { if (
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我正在研究用 GCC 编译的 ARM C/C++ 代码的性能优化。 CPU 是 Tegra 3。 据我所知标志-mthumb表示生成旧的 16 位 Thumb 指令。在不同的测试中,-marm 的性能
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我正要调试别人的代码,我偶然发现了一种处理全局数组的“方式”,我认为这种方式非常糟糕,但第一个使用它的人发誓要这样做。我需要找到反对它的论据。这是简化后的代码(这不是原始代码,只是一个抽象版本) 所以
select col1,col2,col3,col4,col5 from table1 where (col1 like '%keyword%' or col2 like '%keywo
我发现我经常进行递归调用只是为了重新排列参数。 例如,这是我对 endOther from codingbat.com 的解决方案: Given two strings, return true if
首先,我知道在复制文件时,如果缓冲区大小越大,复制过程就会越快。 以下 ruby 脚本用于逐行编写(即缓冲区的大小将取决于行的长度): input,output = ARGV[0], ARGV[1]
我是一名优秀的程序员,十分优秀!