0

when I create a "acrptr" object inside another "arcptr" object,the application returned with error code,and I found that when I remove the "delete" in the destructor,everything is fine.I can't understand why...

template <class T>
class arcptr {
public:
    arcptr()
        : ptr(nullptr)
        , originPointer(false){};
    arcptr(T* ptr)
        : ptr(ptr)
        , originPointer(true){};
    arcptr(const arcptr<T>& it)
        : ptr(it.ptr)
        , originPointer(false){};
    ~arcptr()
    {
        if (originPointer)
            delete ptr;
    };
    T operator*() { return *ptr; };
    T* operator->() { return ptr; };

private:
    T* ptr;
    bool originPointer;
};

,

    class A {
public:
    A() {};
private:
    int value;
};

class B {
public:
    B() {
        myA = new A;
    }
private:
    arcptr<A> myA;
};

int main() {
    arcptr<B> myB = new B;
}
dsukrect
  • 65
  • 6

0 Answers0