C++'s this() equivalent?

zjh fqbqrr at 163.com
Fri Jun 16 00:31:31 UTC 2023


On Thursday, 15 June 2023 at 21:48:10 UTC, Ali Çehreli wrote:
>
> And this is by design: In D, we rename the type and be done 
> with it. In C++, one needs to rename a number of functions as 
> well.
>
> Ali


```d
     static int e=40;
     struct B{
         int m;
         this(int i){
             printf("%i",e);m=i;
         }
         ~this(){
             e=m;
             printf("%i",e);
         }
//After leaving the scope, restore the original value
     }
// definition,
// usage
     void f(){
         B b=e;e=6;
     }//e Restore to 40.
```
But I want like follow, but due to the `disable this()`,I can't:
```d
     void f(){
         B b;e=6;
     }
```
Because disabled `this()`, the behavior of `this(int i)`is 
inconsistent and cannot achieve similar `RAII` as `C++`. What 
should I do?



More information about the Digitalmars-d-learn mailing list