Invalid unique_ptr behavior in vs2013

I wonder if anyone can explain why unique_ptr breaks inheritance access, for example:

class Base {

};

class Derived : private Base {

};

int main() {
    Base* pointer = new Derived;
    return 0;
}

      

This will result in a compile-time error because Derived inherits the base using the private keyword. Therefore, no one from Derived needs to know the relationship between Derived and Base. But if I do this:

unique_ptr<Base> pointer = unique_ptr<Base>(new Derived);

      

Instead:

Base* pointer = new Derived;

      

The code compiles and works fine ...

+3


source to share





All Articles