:: new是什么意思? [英] What does ::new mean?

查看:182
本文介绍了:: new是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在检查MS directX 11 DXUT示例时,出现以下代码:

When examining the MS directX 11 DXUT example, the following code appeared:

template<typename TYPE> HRESULT CGrowableArray <TYPE>::SetSize( int nNewMaxSize )
{
int nOldSize = m_nSize;

if( nOldSize > nNewMaxSize )
{
    assert( m_pData );
    if( m_pData )
    {
        // Removing elements. Call dtor.

        for( int i = nNewMaxSize; i < nOldSize; ++i )
            m_pData[i].~TYPE();
    }
}

// Adjust buffer.  Note that there's no need to check for error
// since if it happens, nOldSize == nNewMaxSize will be true.)
HRESULT hr = SetSizeInternal( nNewMaxSize );

if( nOldSize < nNewMaxSize )
{
    assert( m_pData );
    if( m_pData )
    {
        // Adding elements. Call ctor.

        for( int i = nOldSize; i < nNewMaxSize; ++i )
            ::new ( &m_pData[i] ) TYPE;
    }
}

return hr;
}

这可以在我的DXSDK版本(2010年6月)的428行的DXUTmisc.h中找到.我想知道 :: new 的问题....我正在尝试Google并在堆栈溢出时进行搜索,但是当我键入":: new"时,搜索引擎似乎正在丢弃两个冒号在搜索栏中....

This can be found in DXUTmisc.h on line 428 on my version of DXSDK (June2010). I'm wondering about that ::new thing....I'm trying to Google and search on stack overflow but seems the searching engine is discarding the two colons when I type "::new" in the search bar....

推荐答案

::new调用表示程序正在尝试使用全局new运算符分配空间,而不是使用以前的任何new运算符在类或名称空间范围内定义.特别是,此代码试图使用 展示位置new 将要创建的对象放置在内存中的特定位置.通过显式调用全局范围,该函数可确保此函数正确使用new放置,并且不会意外调用范围链中引入的其他分配函数.

The ::new call means that the program is trying to use the global new operator to allocate space, rather than using any new operator that was defined at class or namespace scope. In particular, this code is trying to use something called placement new in which the object being created is placed at a specific location in memory. By explicitly calling back up into the global scope, the function ensures that this correctly uses placement new and doesn't accidentally call a different allocation function introduced somewhere in the scope chain.

希望这会有所帮助!

这篇关于:: new是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆