[ create a new paste ] login | about

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

C++, pasted on Dec 25:
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
using namespace boost;

struct Melpon {
	Melpon(const char* name) : name_(name) {}
	~Melpon() {cout << name_ << " は死んだ。" << endl;}
	void harahe() {cout << name_ << " ははらへ。" << endl;}
private:
	const char* name_;
};

int main() {
	
    shared_ptr<Melpon> sp(new Melpon("めるぽん"));
    weak_ptr<Melpon> wp(sp);
	
    sp->harahe();
    
    if (shared_ptr<Melpon> p = wp.lock()) {
        p->harahe();
    }
    sp.reset(); // ここで死亡

    if (shared_ptr<Melpon> p = wp.lock()) {
        p->harahe();
    } else {
        cout << "めるぽんは既に死んでる。" << endl;
    }
	
    return 0;
	
}


Output:
1
2
3
4
めるぽん ははらへ。
めるぽん ははらへ。
めるぽん は死んだ。
めるぽんは既に死んでる。


Create a new paste based on this one


Comments: