分享C++三種類型new類型的運算符使用詳情
1.new操作符
new operator,平時用的最多的new操作符,其對應delete operator,不能被重載,其包含兩個操作(既申請空間,又調(diào)用構造函數(shù))
- (1)使用::operator new申請內(nèi)存
- (2)調(diào)用類的構造函數(shù)
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; }
運行結果如下:
2.::operator new
::operator new,只會申請空間,不會調(diào)用構造函數(shù),可以被重載,其對應 ::operator delete (只申請空間)
2.1原始調(diào)用
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; }
運行結果如下:
2.2重載1
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; }
運行結果如下:
2.3重載2
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; }
運行結果如下:
3.place new
place new在已經(jīng)構建好的內(nèi)存中創(chuàng)建對象,其只會返回已經(jīng)申請好的內(nèi)存指針,多用在高性能場景下,提前申請好內(nèi)存,可以節(jié)省申請內(nèi)存開消 (只調(diào)用構造函數(shù))
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; //調(diào)用全局::new (pChar) CTestUse4,new (pChar) CTestUse4調(diào)用重載new ? ? //delete pCase; ? ? pCase->~CTestUse4(); } ? int main(int argc, char *argv[]) { ? ? test4(); ? ? ? return 0; }
運行結果如下:
到此這篇關于C++三種類型new運算符的使用詳情的文章就介紹到這了,更多相關C++ new運算符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Qt?加載?libjpeg?庫出現(xiàn)“長跳轉已經(jīng)運行”錯誤問題解決
這篇文章主要介紹了Qt?加載?libjpeg?庫出現(xiàn)“長跳轉已經(jīng)運行”錯誤,本文給大家分享完美解決方案,需要的朋友可以參考下2023-04-04