Namespaces
Variants
Views
Actions

Talk:cpp/memory/unique ptr/unique ptr

From cppreference.com

#include <iostream>
#include <memory>
using namespace std;
struct A <br>
{
        ~A() { std:: cout << __FUNCTION__; }
};

struct B : public A
{
        ~B() { std:: cout << __FUNCTION__; }
};

int main() {
        unique_ptr<A> a(unique_ptr<B>(new B));
        // compare to
        shared_ptr<A> a(new B);
        return 0;
}

Here ~B() is not called with unique_ptr, but does with shared_ptr (Tested with gcc-4.8.1). d-tors are not virtual. Why is it normal for unique_ptr? Why does it loose deleter?

Consider asking programming questions at programming forums such as StackOverflow, although the only answer you might get is that since A::~A() is not virtual, the behavior of this program is undefined. As far as unique_ptr's deleters go, you requested std::default_delete<A> by writing unique_ptr<A> a --Cubbi (talk) 06:32, 22 November 2013 (PST)

[edit] numbering inconsistency

The constructores are numbered (1) (1) again! (2) ... (7), but in the examples that are numbered (1) ... (8). It seems thatthe examples that output (6), in fact the second constructor numbered (1) is used; after that there is a shift between the remaining examples and the numbering of the constructors (6), (7). This shold be corrected. Marc van Leeuwen (talk) 23:29, 18 June 2014 (PDT)

indeed, the nullptr constructor which used to have number 6 was renumbered 1 in april last year, but the example still follows the old numbering. I'll fix. --Cubbi (talk) 06:50, 19 June 2014 (PDT)