Joh*_*ick 3 c++ string exception
我在Java中做了很多...
String something = "A default value.";
try {
something = this.aFunctionThatMightThrowAnException();
} catch (Exception ignore) { }
this.useTheString(something);
Run Code Online (Sandbox Code Playgroud)
现在我正试图找到一个等效的方法std::string.这是我的......
std::string something("A defualt value.");
try {
something = this->aFunctionThatMightThrowAnException();
} catch (const std::exception& ignore) { }
this->useTheString(something);
Run Code Online (Sandbox Code Playgroud)
为了完整起见,这aFunctionThatMightThrowAnException()可能是......
std::string MyClass::aFunctionThatMightThrowAnException() {
/* Some code that might throw an std::exception. */
std::string aString("Not the default.");
return aString;
}
Run Code Online (Sandbox Code Playgroud)
我有三个关于C++版本的问题:
something传入aFunction作为参考更常见?something从aFunction...安全返回吗?具体是最初分配给"A default value."发布的内存? 这是解决这类问题的方法吗?
是.
或者将某些内容作为参考传递给aFunction更常见?
没有.
我的任务是作为从函数返回的东西......安全吗?具体是最初分配给"默认值"的内存.释放?
是.
在抛出异常的情况下是否有我无法看到的副作用?
没有.