数据初始化(这是正确的吗?)

编程入门 行业动态 更新时间:2024-10-25 11:19:35
本文介绍了数据初始化(这是正确的吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道s2上的以下操作是否合法。 如果不是我应该做什么(我的数据成员是在 $ b $中声明的b结构的开始)。 我很确定这是非法的,但我想避免重复初始化s1时发生的逻辑。 。 > 感谢您的帮助。 Mathieu (*) #include< iostream> struct String { char内部[4 + 1]; 无效打印(){ std :: cout<<内部<< std :: endl; } }; int main() { String s1 = {" 019Y"}; //如果太大则给出编译错误 s1.Print(); String s2; char array [] = 123D; s2 = reinterpret_cast< String&>(array); //这是合法的 s2.Print(); 返回0; }

I would like to know if the following operation on s2 is legal or not. And if not what should I do instead (my data member are declared at the begining of the struct). I am pretty sure this is illegal, but I would like to avoid duplicating the logic that take place when initializing s1. Thanks for your help. Mathieu (*) #include <iostream> struct String { char Internal[4+1]; void Print() { std::cout << Internal << std::endl; } }; int main() { String s1 = {"019Y"}; // give compiler error if too large s1.Print(); String s2; char array[] = "123D"; s2 = reinterpret_cast<String&>( array ); // is this legal s2.Print(); return 0; }

推荐答案

文章< 1148696512.476379.294630 @ i39g2000cwa.googlegroups>, ma *************** @ gmail 说... [...] In article <1148696512.476379.294630 @i39g2000cwa.googlegroups>, ma***************@gmail says... [ ... ] String s2; char array [] =" 123D"; s2 = reinterpret_cast< String&>(array); //这是合法的 String s2; char array[] = "123D"; s2 = reinterpret_cast<String&>( array ); // is this legal

这取决于你所说的合法。如果这就是你的意思,那就不会给定定义的结果。 - 后来, 杰瑞。 宇宙是自己想象的虚构。

That depends on what you mean by "legal". It doesn''t give defined results, if that''s what you mean. -- Later, Jerry. The universe is a figment of its own imagination.

mathieu写道: mathieu wrote: 我想知道s2上的以下操作是否合法。如果不是我应该做什么(我的数据成员是在开头宣布的struct) struct String {内部[4 + 1]; void Print(){ std :: cout< <内部<< std :: endl; } }; 有点奇怪的结构 - 这是你真正想要使用的东西吗?或者是一个愚蠢的例子?如果是后者,那很好。如果 前......重新考虑。 String s1 = {" 019Y"}; //如果太大则给出编译器错误 s1.Print(); 字符串s2; char array [] =" 123D"; s2 = reinterpret_cast< ; String&>(数组); //这是合法的 我很确定这是非法的 这在技术上并不违法,但与任何使用reinterpret_cast一样, 无法保证结果如何。这是 100%实现定义和不可移植。无论你想要什么,要实现,reinterpret_cast都不是这样的。 但是我想避免重复初始化s1时发生的逻辑。 I would like to know if the following operation on s2 is legal or not. And if not what should I do instead (my data member are declared at the begining of the struct). struct String { char Internal[4+1]; void Print() { std::cout << Internal << std::endl; } }; Kind of an odd struct -- is this something you''re actually attempting to use, or a dumbed-down example? If the latter, fine. If the former... reconsider. String s1 = {"019Y"}; // give compiler error if too large s1.Print(); String s2; char array[] = "123D"; s2 = reinterpret_cast<String&>( array ); // is this legal I am pretty sure this is illegal It''s not technically illegal, but as with any use of reinterpret_cast, there is no guarantee whatsoever as to what the result could be. It''s 100% implementation-defined and non-portable. Whatever you''re trying to achieve, reinterpret_cast is not the way. but I would like to avoid duplicating the logic that take place when initializing s1.

你能解释一下你的意思吗?说起来没有任何逻辑 。您是否尝试优化速度?避免代码 重复?我不认为这里发生过任何一件事。 卢克

Could you explain what you mean by this? There''s not really any logic to speak of. Are you attempting to optimize for speed? Avoid code duplication? I don''t think either is happening here. Luke

mathieu张贴: mathieu posted: s2 = reinterpret_cast< String&>(array); //这是合法的 s2 = reinterpret_cast<String&>( array ); // is this legal

是的,它完全没问题。我会告诉你原因。首先,标准 保证以下内容完全合法: struct Monkey { int array [10]; }; int main() { Monkey obj; int(& array)[10] = reinterpret_cast< int(&)[10]>(obj); array [3] = 7; } 因此反向必须为真: struct Monkey { int array [10]; }; int main() { int array [10]; Monkey& monkey = reinterpret_cast< Monkey&>(array); monkey.array [3] = 7; } 但是,这仅适用于POD。这是来自 标准的相关段落: 9.2 p17:指向POD结构的指针,使用进行适当转换 reinterpret_cast,指向其初始成员(如果该成员是一个位 - 字段,然后指向它所在的单位),反之亦然。 -Tomás

Yes, it''s perfectly okay. I''ll show you why. Firstly the Standard guarantees that the following is pefectly legal: struct Monkey { int array[10]; }; int main() { Monkey obj; int (&array)[10] = reinterpret_cast<int (&)[10]>(obj); array[3] = 7; } Therefore the reverse must be true: struct Monkey { int array[10]; }; int main() { int array[10]; Monkey &monkey = reinterpret_cast<Monkey &>(array); monkey.array[3] = 7; } However, this only holds true for a POD. Here''s the relevant passage from the Standard: 9.2 p17: A pointer to a POD-struct, suitably converted using a reinterpret_cast, points to its initial member (of if that member is a bit- field, then to the unit in which it resides) and vice versa." -Tomás

更多推荐

数据初始化(这是正确的吗?)

本文发布于:2023-11-10 06:10:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这是   初始化   正确   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!