gpt4 book ai didi

c++ - 构造函数中止构造

转载 作者:太空狗 更新时间:2023-10-29 23:24:28 24 4
gpt4 key购买 nike

我想让构造函数在遇到某些错误代码时中止对象构造(例如,如果遇到以下情况):

CudaObj::CudaObj(InsertionSim *theSim)
{
// Setup
if(cublasInit() == CUBLAS_STATUS_NOT_INITIALIZED) {
printf("CUBLAS init error.\n");
return -1; // abort here rather than return a value
}

...
}

要实现这一点,最简单的方法是什么?会不会是异常处理?

最佳答案

我认为惯用的方法是从构造函数中抛出异常以强调对象不处于有效状态。

关于c++ - 构造函数中止构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1622546/

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