- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想知道标准对下面这段代码的看法。临时对象的string
析构函数能否在调用printPointer
之前执行?
附注VS2010 编译器不会提示此代码并且可以正常工作。
void printPointer(const string* pointer)
{
cout << *pointer << endl;
}
const string* func(const string& s1)
{
return &s1;
}
int main()
{
printPointer(func("Hello, World!!!"));
}
最佳答案
Can
string
destructor of temporary object be executed before callingprintPointer
?
不,因为临时对象将作为评估包含它们创建点的完整表达式的最后一步被销毁,这意味着它会一直存在,直到调用 printPointer ()
结束。
来自标准#12.2/4 Temporary objects[class.temporary] :
Temporary objects are destroyed as the last step in evaluating the full-expression ([intro.execution]) that (lexically) contains the point where they were created.
和#12.2/6 Temporary objects[class.temporary] :
A temporary object bound to a reference parameter in a function call ([expr.call]) persists until the completion of the full-expression containing the call.
关于c++ - 函数调用中的隐式析构函数执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38345484/
我是一名优秀的程序员,十分优秀!