gpt4 book ai didi

C++ 异常 :Throwing Arrays and getting array size in catch

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:03 25 4
gpt4 key购买 nike

普通函数(例如 printArray)采用数组及其大小(2 个参数)来打印数组元素。

如何使用异常来做同样的事情?更确切地说,如何将数组大小传递给 catch 处理程序? (假设我没有在 try-catch 之外声明的 const int SIZE)例如。

 //void printArray(int* foo ,int size);
int foo[] = { 16, 2, 77, 40, 12071 };
//printArray(foo,5); //OK, function call using array accepts size=5
try{

//do something
throw foo;

}
catch (int* pa)
{
//I have to get array size to loop through and print array values
// How to get array size?
}

提前致谢

最佳答案

您可以通过以下方式将数组及其大小成对抛出:

throw std::make_pair(foo, 5);

并像这样获取这两个值:

catch(std::pair<int*, int>& ex)
{
...
}

关于C++ 异常 :Throwing Arrays and getting array size in catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30069382/

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