C++ Logo

std-proposals

Advanced search

Re: [std-proposals] New type 'dvoid' -- a pointer to a destructible object

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 23 Aug 2022 16:19:21 +0100
On Tue, Aug 23, 2022 at 12:28 PM Frederick Virchanza Gotham
<cauldwell.thomas_at_[hidden]> wrote:
>
> class ISmartPtr {
> public:
> virtual ~ISmartPtr(void);
> };
>
> class unique_ptr : public ISmartPtr { . . . };
> class shared_ptr : public ISmartPtr { . . . };
> class weak_ptr : public ISmartPtr { . . . };
>
> So this would mean that we could have a container that automatically
> destroys any kind of smart pointer as follows:
>
> vector<ISmartPtr> my_vector;


Of course this isn't going to work because of object slicing. I mean
if you do the following:

    vector<ISmartPtr> my_vector;
    my_vector.push_back( unique_ptr<SomeClass>(new SomeClass) );

then the object inside the vector will just use the destructor of
"ISmartPr" (assuming it's not pure virtual of course).

I was writing this all up and I got confused. I'm writing it out properly now.

Received on 2022-08-23 15:19:33