目录1.new操作符2.::operator new2.1原始调用2.2重载12.3重载23.place new1.new操作符 new operator,平时用的最多的new操作符
new operator,平时用的最多的new操作符,其对应delete operator,不能被重载,其包含两个操作(既申请空间,又调用构造函数)
class CTestUse
{
public:
CTestUse() { cout << __FUNCTioN__ << endl; }
virtual ~CTestUse() { cout << __FUNCTION__ << endl; }
private:
};
void test()
{
string *pStr = new string("new operator");
cout << *pStr << endl;
delete pStr;
CTestUse *pCase = new CTestUse;
delete pCase;
}
int main(int arGC, char *argv[])
{
test();
return 0;
}
运行结果如下:
::operator new,只会申请空间,不会调用构造函数,可以被重载,其对应 ::operator delete (只申请空间)
class CTestUse1
{
public:
CTestUse1() { cout << __FUNCTION__ << endl; }
virtual ~CTestUse1() { cout << __FUNCTION__ << endl; }
void show() { cout << __FILE__ << " " << __FUNCTION__ << endl; }
private:
};
void test1()
{
CTestUse1 *pCase = (CTestUse1 *)::operator new (sizeof(CTestUse1));
pCase->show();
::operator delete(pCase);
}
int main(int argc, char *argv[])
{
test1();
return 0;
}
运行结果如下:
class CTestUse2
{
public:
CTestUse2() { cout << __FUNCTION__ << endl; }
virtual ~CTestUse2() { cout << __FUNCTION__ << endl; }
void show() { cout << __FILE__ << " " << __FUNCTION__ << endl; }
void* operator new(size_t size) {
cout << __FUNCTION__ << endl;
return ::operator new(size);
}
void operator delete(void* ptr) {
cout << __FUNCTION__ << endl;
::operator delete(ptr);
}
private:
};
void test2()
{
CTestUse2 *pCase = new CTestUse2;
pCase->show();
delete pCase;
}
int main(int argc, char *argv[])
{
test2();
return 0;
}
运行结果如下:
class CTestUse3
{
public:
CTestUse3() { cout << __FUNCTION__ << endl; }
virtual ~CTestUse3() { cout << __FUNCTION__ << endl; }
void show() { cout << __FILE__ << " " << __FUNCTION__ << endl; }
void* operator new(size_t size, string str) {
cout << __FUNCTION__ << str << endl;
return ::operator new(size);
}
void operator delete(void* ptr) {
cout << __FUNCTION__ << endl;
::operator delete(ptr);
}
private:
};
void test3()
{
CTestUse3 *pCase = new ("heshiyang") CTestUse3;
pCase->show();
delete pCase;
}
int main(int argc, char *argv[])
{
test3();
return 0;
}
运行结果如下:
place new在已经构建好的内存中创建对象,其只会返回已经申请好的内存指针,多用在高性能场景下,提前申请好内存,可以节省申请内存开消 (只调用构造函数)
class CTestUse4
{
public:
CTestUse4() { cout << __FUNCTION__ << endl; }
virtual ~CTestUse4() { cout << __FUNCTION__ << endl; }
void show() { cout << __FILE__ << " " << __FUNCTION__ << endl; }
void* operator new(size_t size, void* p) {
cout << __FUNCTION__ << endl;
return p;
}
void operator delete(void* ptr) {
cout << __FUNCTION__ << endl;
::operator delete(ptr);
}
private:
};
void test4()
{
char* pChar = new char[sizeof(CTestUse4)];
CTestUse4* pCase = new (pChar) CTestUse4; //调用全局::new (pChar) CTestUse4,new (pChar) CTestUse4调用重载new
//delete pCase;
pCase->~CTestUse4();
}
int main(int argc, char *argv[])
{
test4();
return 0;
}
运行结果如下:
到此这篇关于c++三种类型new运算符的使用详情的文章就介绍到这了,更多相关C++ new运算符内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 分享C++三种类型new类型的运算符使用详情
本文链接: https://lsjlt.com/news/145505.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0