我需要解决一个错误,编译器捡-我明白为什么它捡了该错误,但需要解决它,因为函数(投掷)中的错误时,指针将只执行被初始化.
这是我的伪代码:
if (incoming_message_exists)
{
msg_class* current_msg;
/*current_msg will become either value_1 or value_2*/
/*code block 1*/
if (condition_is_fulfilled)
{
current_msg = value_1;
}
/*code block 2*/
else
{
current_msg = value_2;
}
/*code block 3*/
/*bool function performed on current_msg that is throwing error*/
if (function(current_msg))
{
//carry out function
}
}
Run Code Online (Sandbox Code Playgroud)
我宁愿不在1和2内执行代码块3,但如果这是唯一的解决方案,那么我会.提前致谢!
是if和else您展示我们分行从两个不同的if语句?
如果是,您当前的代码能够保持current_msg未初始化状态.当你到达时,这可能会崩溃function(current_msg).
如果你为同一个if语句向我们展示了两个分支,那么你的编译器是错误的 - 没有current_msg不被初始化的危险.您可能仍需要更改代码以禁止警告,例如,如果您将警告构建为错误.
您可以通过初始化current_msg声明来确定/禁止警告
msg_class* current_msg = NULL;
Run Code Online (Sandbox Code Playgroud)
如果在任一分支中都没有其他代码,则还可以使用三元运算符进行初始化
msg_class* current_msg = condition_is_fulfilled? value_1 : value_2;
Run Code Online (Sandbox Code Playgroud)
如果警告是真实的,你还必须检查是否应该function通过NULL辩论或防范此事
if (current_msg != NULL && function(current_msg))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |