gpt4 book ai didi

c++ - 在 C++ 中同时分配和条件测试

转载 作者:搜寻专家 更新时间:2023-10-31 00:22:05 24 4
gpt4 key购买 nike

我有三个返回整数错误代码的函数,例如

int my_function_1(const int my_int_param);
int my_function_2(const int my_int_param);
int my_function_3(const int my_int_param);

为简洁起见,我想同时分配和测试错误。以下将工作并可移植吗?

int error=0;
...
if ( error ||
(error = my_function_1(val1) ||
error = my_function_2(val2) ||
error = my_function_3(val3)) ) {
std::cout << "AN ERROR OCCURRED!!!" << std::endl;
}

谢谢!

最佳答案

为什么不抛出异常?

void my_function_1(const int my_int_param);
void my_function_2(const int my_int_param);
void my_function_3(const int my_int_param);

try {
my_function_1(...);
my_function_2(...);
my_function_3(...);
} catch(std::exception& e) {
std::cout << "An error occurred! It is " << e.what() << "\n";
}

关于c++ - 在 C++ 中同时分配和条件测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3736911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com