C++类与new和delete操作符
生活随笔
收集整理的這篇文章主要介紹了
C++类与new和delete操作符
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
操作符delete和delete[]在釋放對象存儲空間的同時也會調(diào)用析構(gòu)函數(shù),而free函數(shù)則不會調(diào)用析構(gòu)函數(shù)。
#include<iostream> using namespace std;class test { public:test(int i = 1){num = i;cout<<num<<" Constructor"<<endl;}~test(){cout<<num<<" Destructor"<<endl;} private:int num; };int main() {test * t0 = new test(0);test * t1 = new test[5];test * t2 = (test *)malloc(sizeof(test));delete t0;delete[] t1;free(t2);return 0; }?
總結(jié)
以上是生活随笔為你收集整理的C++类与new和delete操作符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++常量指针this
- 下一篇: C++类与const关键字