是否可以在C++;? 是否可以在C++中动态分配临时变量? 我想这样做: #include <iostream> #include <string> std::string* foo() { std::string ret("foo"); return new std::string(ret); } int main() { std::string *str = foo(); std::cout << *str << std::endl; return 0; }

是否可以在C++;? 是否可以在C++中动态分配临时变量? 我想这样做: #include <iostream> #include <string> std::string* foo() { std::string ret("foo"); return new std::string(ret); } int main() { std::string *str = foo(); std::cout << *str << std::endl; return 0; },c++,pointers,new-operator,C++,Pointers,New Operator,这个怎么样: std::string* foo() { std::string * ret = new std::string("foo"); // do stuff with ret return ret; } 这个怎么样: std::string* foo() { std::string * ret = new std::string("foo"); // do stuff with ret return ret; } 是的,有,它被称为智

这个怎么样:

std::string* foo()
{
    std::string * ret = new std::string("foo");
    // do stuff with ret
    return ret;
}
这个怎么样:

std::string* foo()
{
    std::string * ret = new std::string("foo");
    // do stuff with ret
    return ret;
}

是的,有,它被称为智能指针:

#include <memory>
std::unique_ptr<std::string> foo()
{
    return std::unique_ptr<std::string>("foo");
}

// Use like this:
using namespace std;
auto s = foo();     // unique_ptr<string> instead of auto if you have an old standard.
cout << *s << endl; // the content pointed to by 's' will be destroyed automatically
                    // when you stop using it
#包括
std::unique_ptr foo()
{
返回std::unique_ptr(“foo”);
}
//这样使用:
使用名称空间std;
自动s=foo();//如果您使用的是旧标准,则使用unique_ptr而不是auto。

cout是的,它被称为智能指针:

#include <memory>
std::unique_ptr<std::string> foo()
{
    return std::unique_ptr<std::string>("foo");
}

// Use like this:
using namespace std;
auto s = foo();     // unique_ptr<string> instead of auto if you have an old standard.
cout << *s << endl; // the content pointed to by 's' will be destroyed automatically
                    // when you stop using it
#包括
std::unique_ptr foo()
{
返回std::unique_ptr(“foo”);
}
//这样使用:
使用名称空间std;
自动s=foo();//如果您使用的是旧标准,则使用unique_ptr而不是auto。


难道
std::string foo(){return“foo”;}
有什么问题吗?副本几乎可以保证被删除。对James来说+1,但避免“除了”这对我们这些非英语母语人士来说很困惑。我的代码比这复杂得多,指针约束是无法避免的。那么,也许智能指针会有所帮助,尤其是在许多情况下,您需要能够释放该变量。@kl94:如果您的示例没有说明您的问题,您能否提供一个示例,说明为什么您必须返回堆分配的内存?std::string foo(){return“foo”;}
有什么问题吗?副本几乎可以保证被删除。对James来说+1,但避免“除了”这对我们这些非英语母语人士来说很困惑。我的代码比这复杂得多,指针约束是无法避免的。那么,也许智能指针会有所帮助,尤其是在许多情况下,您需要能够释放该变量。@kl94:如果您的示例没有说明您的问题,您能否提供一个示例,说明为什么您必须返回堆分配的内存?当然,这段代码是有效的。一开始,我已经这样做了,但是我必须对我的var做一些事情。但是我有很多异常要检查,我必须在每次抛出之前删除我的var。所以我决定创建一个局部变量,然后在测试之后,返回它的指针。为了完成我的回答,即使你抛出,智能指针也会被删除。当然,这段代码是有效的。一开始,我已经这样做了,但是我必须对我的var做一些事情。但是我有很多异常要检查,我必须在每次抛出之前删除我的var。所以我决定创建一个局部变量,然后在测试之后,返回它的指针。为了完成我的回答,即使你抛出,智能指针也会被删除。(我假设你想要动态分配的内存,如果你不按照James McNellis提到的值返回),这是一个很好的方法,但是有没有一种不改变返回类型的方法呢?(标准::字符串*)?我正在考虑使用dynamic_cast,但我无法使其正常工作。不幸的是,我无法使用您的解决方案,因为我使用的是旧标准。但是谢谢您的帮助。如果您可以使用boost,boost会在旧标准中提供boost::unique\u ptr,这是新标准的灵感来源。@J.N:不,boost不提供
unique\u ptr
。它提供了
shared_ptr
scoped_ptr
,但
unique_ptr
需要移动语义,而Boost在完成大部分工作时不存在移动语义。甚至Boost.Move也没有提供独特的实现。您应该建议使用
auto\u ptr
,这是一种功能完善的智能指针(如果您不知道自己在做什么,则很危险)。(我假设您需要动态分配的内存,如果您没有按照James McNellis提到的那样按值返回),这是一种很好的方法,但是有没有一种方法可以在不更改返回类型的情况下实现这一点?(标准::字符串*)?我正在考虑使用dynamic_cast,但我无法使其正常工作。不幸的是,我无法使用您的解决方案,因为我使用的是旧标准。但是谢谢您的帮助。如果您可以使用boost,boost会在旧标准中提供boost::unique\u ptr,这是新标准的灵感来源。@J.N:不,boost不提供
unique\u ptr
。它提供了
shared_ptr
scoped_ptr
,但
unique_ptr
需要移动语义,而Boost在完成大部分工作时不存在移动语义。甚至Boost.Move也没有提供独特的实现。您应该建议使用
auto_ptr
,这是一种功能完善的智能指针(如果您不知道自己在做什么,则很危险)。