c ++取临时地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3339879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ taking address of temporary
提问by zigi
I've simple class method like
我有简单的类方法,如
Task TaskSample::Create(void)
{
Task task;
return task;
}
and got warning taking address of temporary. Is something wrong with this code ? I prefer to not use pointer here
并收到警告取临时地址。这段代码有问题吗?我宁愿不在这里使用指针
回答by Billy ONeal
If that is actually what your code is, then the compiler is probably in error.
如果这实际上是您的代码,那么编译器可能出错了。
More likely, however, you actually wrote this:
然而,更有可能的是,您实际上是这样写的:
Task& TaskSample::Create(void)
{
Task task;
return task;
}
Remove the & to return by value, instead of by reference. Returning by reference there makes no sense because task
will be destroyed when the function returns.
删除 & 以按值返回,而不是按引用返回。按引用返回没有意义,因为task
函数返回时将被销毁。
回答by Igor Zevaka
Both of the following code snippets produce this error in MS C++:
以下两个代码片段在 MS C++ 中都会产生此错误:
warning C4172: returning address of local variable or temporary
警告 C4172:返回局部变量或临时变量的地址
Task* Create(void)
{
Task task;
return &task;
}
Task& Create2(void)
{
Task task;
return task;
}
MSDN documentationdescribes the warning quite succintly:
MSDN 文档非常简洁地描述了警告:
Local variables and temporary objects are destroyed when a function returns, so the address returned is not valid.
函数返回时局部变量和临时对象被销毁,因此返回的地址无效。
In order to return a pointer to an object you need to call operator new
as an object allocated on the heap will not go out of scope:
为了返回指向您需要调用operator new
的对象的指针,因为在堆上分配的对象不会超出范围:
Task* Create(void)
{
Task* task = new Task();
return task;
}
Don't forget to delete
that task once you are done with it:
完成后不要忘记执行delete
该任务:
Task* task = Create();
delete task;
Alternatively you can use a smart pointer:
或者,您可以使用智能指针:
void Test () {
boost::scoped_ptr<Task> spTask = Create();
spTask->Schedule();
} //<--- spTask is deleted here
I would instead rely on RVOand actually use the code that you posted and which is most likely not the code giving you a warning.
相反,我会依赖RVO并实际使用您发布的代码,这很可能不是给您警告的代码。
void Test() {
Task task = Create();
}
Task Create(void)
{
Task task;
task.start = 10;
return task;
}
This may generate something equivalent to this, so really, there is no copy constructor overhead.
这可能会生成与此等效的东西,因此实际上,没有复制构造函数开销。
void Test() {
Task task;
Create(&task);
}
Task* Create(Task* __compilerGeneratedParam)
{
__compilerGeneratedParam->start = 10;
return __compilerGeneratedParam;
}
回答by user401947
Modern compilers can optimize return value to avoid copying overhead. Often returning by value doesn't hurt performance at all.
现代编译器可以优化返回值以避免复制开销。通常按值返回根本不会影响性能。
But if you need to return by reference, use shared_ptr instead.
但是如果您需要通过引用返回,请改用 shared_ptr。
shared_ptr<Task> TaskSample::Create(void)
{
shared_ptr<Task> ptr(new Task(...));
return ptr;
}
回答by nsivakr
The best would be, to pass the object by reference as follows
最好的办法是,按如下方式通过引用传递对象
void TaskSample::Create(Task& data)
{
....
}