digitalmars.D.learn - Calling copyctor manually
- SrMordred (31/31) May 31 2019 Its possible to call copyctor manually without calling dtor?
 - Paul Backus (18/49) Jun 01 2019 The copy constructor is implemented as an overload of `__ctor`:
 - SrMordred (3/10) Jun 01 2019 Thanks!
 
Its possible to call copyctor manually without calling dtor?
ex, what i did before:
struct T{ ~this(){ writeln("DTOR"); } this(this){ 
writeln("POSTBLIT"); } }
T a;
T b;
memcpy(&a,&b,T.sizeof);
a.__postblit;
/*
output:
POSTBLIT
DTOR
DTOR
*/
With copy ctors, not sure what to do.
struct T{
~this(){ writeln("DTOR"); } 	
this(ref return scope T self){ writeln("COPYCTOR"); } }
T a;
T b;
memcpy(&a,&b,T.sizeof);
a.opAssign(b); //???
//same as a = b;
/*
output:
COPYCTOR
DTOR
DTOR
DTOR
*/
I want something like '.__xcopyctor'
 May 31 2019
On Saturday, 1 June 2019 at 02:27:36 UTC, SrMordred wrote:
 Its possible to call copyctor manually without calling dtor?
 ex, what i did before:
 struct T{ ~this(){ writeln("DTOR"); } this(this){ 
 writeln("POSTBLIT"); } }
 T a;
 T b;
 memcpy(&a,&b,T.sizeof);
 a.__postblit;
 /*
 output:
 POSTBLIT
 DTOR
 DTOR
 */
 With copy ctors, not sure what to do.
 struct T{
 ~this(){ writeln("DTOR"); } 	
 this(ref return scope T self){ writeln("COPYCTOR"); } }
 T a;
 T b;
 memcpy(&a,&b,T.sizeof);
 a.opAssign(b); //???
 //same as a = b;
 /*
 output:
 COPYCTOR
 DTOR
 DTOR
 DTOR
 */
 I want something like '.__xcopyctor'
The copy constructor is implemented as an overload of `__ctor`:
import std.stdio;
struct T {
     ~this() { writeln("DTOR"); }
     this(ref return scope T self) { writeln("COPYCTOR"); }
}
void main() {
     T a;
     T b;
     a.__ctor(b);
}
/* Output:
COPYCTOR
DTOR
DTOR
*/
https://run.dlang.io/is/NeioBs
 Jun 01 2019
On Saturday, 1 June 2019 at 19:10:36 UTC, Paul Backus wrote:On Saturday, 1 June 2019 at 02:27:36 UTC, SrMordred wrote:void main() { T a; T b; a.__ctor(b); } https://run.dlang.io/is/NeioBsThanks! The most obvious way i didn“t think :P
 Jun 01 2019








 
 
 
 SrMordred <patric.dexheimer gmail.com>