[ create a new paste ] login | about

Link: http://codepad.org/H1hpVfhR    [ raw code | output | fork ]

C++, pasted on Jan 16:
#include <vector>
#include <iostream>
using namespace std;

class cItem{
public:
    char *str;    
    cItem(const char * str = 0) {
        if( str )
        strcpy(cItem::str = new char[1 + strlen(str)], str);
        else
        cItem::str = 0;
        cout<<"DFLT CONSTRUCT : "<<(cItem::str ? cItem::str : "null")<<endl;
    }
    cItem(const cItem &pCopy)
    {
        char * str = const_cast< char *>(pCopy.str);
        if( str )
            strcpy(cItem::str = new char[1 + strlen(str)], str);
        else
            cItem::str = 0;
        cout<<"COPY CONSTRUCT : "<<(cItem::str ? cItem::str : "null")<<endl;
    }
    ~cItem(){
        cout<<"DESTRUCT : "<<(cItem::str ? cItem::str : "null")<<endl;
        delete str;
    }
};

int main(){
    cItem itemArr[] = {
        cItem("test1"), 
        cItem("test2"), 
        cItem("test3")};
    vector< cItem > itemVec(itemArr, itemArr + 3);
    cout<<"INITIAL"<<endl;
    cout<<itemVec[0].str<<endl;
    cout<<itemVec[1].str<<endl;
    cout<<itemVec[2].str<<endl;
    cout<<"RESIZE"<<endl;
    itemVec.resize(5);
    cout<<itemVec[0].str<<endl;
    cout<<itemVec[1].str<<endl;
    cout<<itemVec[2].str<<endl;
    cout<<"EOP"<<endl;
    cin.get();
    return 0;
}


Output:
DFLT CONSTRUCT : test1
COPY CONSTRUCT : test1
DESTRUCT : test1
DFLT CONSTRUCT : test2
COPY CONSTRUCT : test2
DESTRUCT : test2
DFLT CONSTRUCT : test3
COPY CONSTRUCT : test3
DESTRUCT : test3
COPY CONSTRUCT : test1
COPY CONSTRUCT : test2
COPY CONSTRUCT : test3
INITIAL
test1
test2
test3
RESIZE
DFLT CONSTRUCT : null
COPY CONSTRUCT : null
COPY CONSTRUCT : null
COPY CONSTRUCT : test1
COPY CONSTRUCT : test2
COPY CONSTRUCT : test3
COPY CONSTRUCT : null
COPY CONSTRUCT : null
DESTRUCT : test1
DESTRUCT : test2
DESTRUCT : test3
DESTRUCT : null
DESTRUCT : null
DESTRUCT : null
test1
test2
test3
EOP
DESTRUCT : test1
DESTRUCT : test2
DESTRUCT : test3
DESTRUCT : null
DESTRUCT : null
DESTRUCT : test3
DESTRUCT : test2
DESTRUCT : test1


Create a new paste based on this one


Comments: