Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

scope new delete

//g++  5.4.0

#include <iostream>
using namespace std;

class Test{
    string name;
    
public:
    
    Test(const char* name):name(name){
        cout<< name <<" be create\n";
    }
    
    ~Test(){
        cout<< name <<" be delete\n";
    }
    
};

int main()
{
    cout<< "program begin!\n";
    
    Test* ptr1 = new Test("ptr1");
    Test* ptr2 = new Test("ptr2");
    
    cout<<"enter for loop\n";
    for(int i=0; i<3; ++i){
        cout<< "Loop "<<i << " times\n";
        
        if(i == 0){
            Test* local = new Test("local");
        }
        
        if(i == 2){
            delete ptr2;
        }
    }
    cout<< "leave for loop\n";
    
    cout<< "program end!\n";
    return 0;
}
 run  | edit  | history  | help 0