您好,登录后才能下订单哦!
这篇文章主要介绍“C++中new类型的运算符怎么使用”,在日常操作中,相信很多人在C++中new类型的运算符怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++中new类型的运算符怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
new operator,平时用的最多的new操作符,其对应delete operator,不能被重载,其包含两个操作(既申请空间,又调用构造函数)
(1)使用::operator new申请内存
(2)调用类的构造函数
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类型的运算符怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。