C++ Logo

std-discussion

Advanced search

Re: DestructorConstructor - a class destructor with parameter

From: Maciej Polanski <maciejpolanski75_at_[hidden]>
Date: Fri, 12 Feb 2021 13:42:53 +0100
On 2021-02-11 20:02, Edward Catmur via Std-Discussion wrote:
> This would operate very similarly to the current proposal, but with
> the possibly unintuitive difference that the new object is being
> passed as the parameter, so that e.g. ~c(x.c) initializes x.c with the
> value of c. Furthermore, it would require re-inventing syntax to allow
> for move construction of subobjects.

While approaches "Constructor that destructs its argument" and
"Destructor that constructs its argument" seems to be just symmetrical,
my DestrcutorConstructor would have one more feature: it allows
"destructing move" depositing object's data in object of other class, or
even a function.

So without judging if this is worth efforts or not, I'd like to
highlight this part of my proposal:

//------
// class to receive data from object being deleted
class cDataSink {
public:
     TakeOverData(cSomeData*);
}

// class that can be deleted with real zero-additional-cost transfer of
data:
class C {
     cSomeData *pDdata;

public:
     ~C(cDataSink* sink) { sink->TakeOverData(data); } // <-
DepositingDestructor here
     ~C() { delete pData; } // Normal
destructor
};
//------

Regards,
Maciej

Received on 2021-02-12 06:42:58