【发布时间】:2013-09-25 15:04:56
【问题描述】:
我想在另一个块中使用 C++ 类对象(在一个块中声明)。有可能这样做吗?让我举一个更具体的例子:
我有一个用户定义的函数 myfunc:
void myfunc()
{
// ...
if(condition is true)
{
myclass *ptr = NULL;
ptr = new myclass // myclass is define somewhere else. Here I am creating an instance of it
}
if(another condition is true)
{
ptr = dosomething
}
} // end of myfunc
我可以在第二个 if 块中使用 ptr 吗?
【问题讨论】:
-
抛开你的实际问题,你不应该使用原始指针。使用智能指针。
-
帮助您进行谷歌搜索:这里的问题是“可变范围”。
标签: c++ oop object dynamic stl