lundi 29 février 2016

C++ runtime error with shared_ptr and builder pattern

I was studying c++ language with shared pointer and builder pattern.

I have written following code that is not working but I don't understand why it emits run-time error.

Could you tell me why it is not working well and how can I solve this problem to work well?

#include <iostream>
#include <memory>
#include <string>

using namespace std;

class Popup
{
public:
    Popup(int value, string str){
        this->v = value;
        this->str = str;
    }
    virtual void print() = 0;
    int v;
    string str;
};
typedef shared_ptr<Popup> PopupPtr;

class PopupA : public Popup
{
public:
    PopupA(int v, string str) : Popup(v, str) { }
    virtual void print() {
        cout << "PopupA" << endl;
    }
};
typedef shared_ptr<PopupA> PopupAPtr;

class PopupB : public Popup
{
public:
    PopupB(int v, string str) : Popup(v, str) { }
    virtual void print() {
        cout << "PopupB" << endl;
    }
};
typedef shared_ptr<PopupB> PopupBPtr;


class Builder
{
public:
    PopupPtr popupPtr;
    Builder() { };
    shared_ptr<Builder> init(int value, string str) {
        shared_ptr<Builder> builder;

        switch (value)
        {
        case 1:
            popupPtr = PopupAPtr(new PopupA(value, str));
            break;
        case 2:
            popupPtr = PopupBPtr(new PopupB(value, str));
            break;
        default:
            cout << "default error" << endl;
            break;
        }

        if (popupPtr) {
            builder = shared_ptr<Builder>(this);
        } 
        else {
            cout << "popup is null" << endl;
        }

        if (!builder) {
            cout << "builder is null" << endl;
        }
        return builder;
    }

    PopupPtr build()
    {
        if (!popupPtr) {
            cout << "popup is null" << endl;
        }
        return PopupPtr(popupPtr);
    }

};
typedef shared_ptr<Builder> BuilderPtr;

int main()
{
    BuilderPtr builderPtr = BuilderPtr(new Builder());

    PopupPtr popupPtr1 = builderPtr->init(1, "111111111111")->build();
    popupPtr1->print();

    PopupPtr popupPtr2 = builderPtr->init(2, "222222222222")->build();
    popupPtr2->print();
    return 0;
}

Thanks in advance for your answers and sorry for my poor english. If you don't understand my question please make a comment.

Aucun commentaire:

Enregistrer un commentaire