我有以下方法:
class MyClass
{
public:
MyClass;
bool method (MyClass &obj);
};
void MyClass::method (MyClass &obj)
{
MyClass *c = new MyClass;
try{
//code
//access another method
return true;
}
catch (std::string s)
{
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我应该在哪里删除之前或之前指向c对象的指针 ?MyClass:return truereturn false
Ste*_*han 10
关于什么:
void MyClass::method (MyClass &obj)
{
MyClass c;
try{
//code
//access another method
return true;
}
catch (std::string s)
{
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
不new- > delete不需要.返回c时自动销毁method.如果你的例子过于简单,你需要创建c与new,你应该遵循与智能指针的建议其他的答案.
| 归档时间: |
|
| 查看次数: |
918 次 |
| 最近记录: |