当在函数调用中使用 new 时,你释放了什么?如下所示的 boost 调用

问题描述 投票:0回答:1

sdt::locale 的函数调用中是否需要有一个与 new 相关联的 free

static boost::posix_time::ptime getPosixTimeFromDateString( string dString )
{
    using namespace boost::posix_time;
    ptime timeDate;
    const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%m-%d"));
    std::istringstream is(dateString);
    is.imbue(loc);

    boost::posix_time::ptime t;
    is >> timeDate;
    return( timeDate );
}

如果不是为什么不,如果是的话你会释放什么,boost对象应该是在函数调用之外创建的吗?

c++ std
1个回答
0
投票

不,因为析构函数负责它。这在文档中明确说明:

Overload (7) 通常使用直接从 new 表达式获取的第二个参数来调用:语言环境负责从其自己的析构函数中调用匹配的删除。

© www.soinside.com 2019 - 2024. All rights reserved.