64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
using namespace std;
class CElement
{
public:
int m_int;
char *p;
public:
CElement():p(NULL){}
~CElement()
{
}
};
template<class T>
class Array
{
private:
typedef T ElementType;
char *m_pBase;
ElementType *m_pArray;
public:
void allocArray(int a_nCount)
{
m_pBase = new char[sizeof(ElementType)*a_nCount];
m_pArray = new(m_pBase) ElementType[a_nCount];
_ASSERTE( _CrtCheckMemory( ) );
}
};
int main()
{
//ProtocalTypeArray<ProtocalTypeStr> test;
//test.allocArray(3);
Array<CElement> m_array;
m_array.allocArray(3);
return 0;
}
m_pArray = new(m_pBase) ElementType;
吗?[/quote]
不好意思,看来是我错了。
我知道Placement new,但是好像不怎么会用。
Mark LS顺便学习下。m_pArray = new(m_pBase) ElementType;
吗?m_pArray = new ElementType[a_nCount];
// 我分析下,主要是这两个写法所导致的区别,我不懂。
不过为什么你要用那样方式new?你是如何考虑的呢?!