检查 MS directX 11 DXUT 示例时,出现以下代码:
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”时,搜索引擎似乎丢弃了两个冒号在搜索栏中....
最佳答案
::new
调用意味着程序正在尝试使用全局 new
运算符来分配空间,而不是使用任何 new
在类或命名空间范围内定义的运算符。特别是,此代码试图使用名为 placement new
的东西。 其中正在创建的对象被放置在内存中的特定位置。通过显式回调到全局作用域,该函数确保它正确地使用了 placement new,并且不会意外调用作用域链中某处引入的不同分配函数。
希望这对您有所帮助!
关于c++ -::new 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14147029/