Jump to page: 1 2
Thread overview
C++'s this() equivalent?
Jun 15, 2023
zjh
Jun 15, 2023
Ali Çehreli
Jun 16, 2023
zjh
Jun 16, 2023
zjh
Jun 16, 2023
zjh
Jun 16, 2023
zjh
Jun 16, 2023
CM
Jun 16, 2023
zjh
Jun 16, 2023
Jonathan M Davis
Jun 16, 2023
zjh
Jun 16, 2023
Jonathan M Davis
Jun 16, 2023
zjh
June 15, 2023
import core.stdc.stdio;

struct A{
    static int e=40;
    struct B{
        int m;
        this(int i){
            printf("%i",e);m=i;
        }
        ~this(){
            e=m;
            printf("%i",e);
        }
    }
    void f(){
        B b=e;e=6;
    }
    void g(){
        printf("%i",e);
    }
}

extern(C)void main(){
    A a;
    a.f();
    a.g();
}

I want B to become the following version similar to 'C++'. What should I do?

struct B{
    int m;
    B(){
        printf("%i",e);m=e;
    }
    ~B(){
        e=m;
        printf("%i",e);
    }
}

Thank you.

June 15, 2023
On 6/15/23 08:15, zjh wrote:

> I want `B` to become the following version similar to 'C++'. What should
> I do?

I have difficulty understanding the question. I think the nested structs, extern(C), static members, etc. confuse me. I am assuming they are not related to this question.

> ```cpp
> struct B{
>      int m;
>      B(){

Do you want to be able to use the name of the user-defined type to mean "constructor", "destructor", etc?

If so, no, it's not possible without preprocessing the text potentially with a C preprocessor.

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

June 16, 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

    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:

    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?

June 15, 2023

On 6/15/23 8:31 PM, zjh wrote:

>

Because disabled this(), the behavior of this(int i)is inconsistent and cannot achieve similar RAII as C++. What should I do?

You cannot have parameter-less constructors in D structs. However, destruction works the same.

Instead, you can use factory functions to initialize.

But there is no way in D to have e.g.:

B b; // runs a constructor

-Steve

June 16, 2023

On Thursday, 15 June 2023 at 21:48:10 UTC, Ali Çehreli wrote:

>

I have difficulty understanding the question. I think the nested structs, extern(C), static members, etc. confuse me. I am assuming they are not related to this question.

>

Ali

I used the class to call the constructor/destructor (destroy), but after they leave the scope, they do not call thedestructor (destroy).
Nested class is because I want to directly use members of external struct, but in reality, I cannot access them directly. Therefore, I only have to use static members,.
extern(C) is to use BetterC,.

The purpose is mainly to complete RAII, completing one part of the work after entering the scope and another part of the work after leaving the scope.

June 16, 2023

On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:

>

But there is no way in D to have e.g.:

B b; // runs a constructor

-Steve

As a C++ user, it is very terrible to simplify RAII without such a method. Why must this() be disabled? Can't there be a constructor? In a constructor, you can complete some work. Without it, how can you simplify RAII?.

June 15, 2023

On 6/15/23 8:45 PM, zjh wrote:

>

On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:

>

But there is no way in D to have e.g.:

B b; // runs a constructor

As a C++ user, it is very terrible to simplify RAII without such a method. Why must this() be disabled? Can't there be a constructor? In a constructor, you can complete some work. Without it, how can you simplify RAII?.

D was not originally designed with RAII. That was added later (D1 structs did not have ctors or dtors).

The benefit of not having parameterless constructors is that default construction is always defined and available at compile time.

What is wrong with using a parameter constructor or factory function? What is the feature you are trying to build that you can with C++, but not with D?

I'm not saying I agree with the limitation, just want to know more.

-Steve

June 16, 2023

On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:

>

Instead, you can use factory functions to initialize.

-Steve

How can factory functions be used to achieve effects similar to 'RAII'?
Thank you.

June 15, 2023
On 6/15/23 8:52 PM, zjh wrote:
> On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:
> 
>> Instead, you can use factory functions to initialize.
>>
> 
> How can `factory functions` be used to achieve effects similar to `'RAII'`?
> Thank you.
> 

B b;

becomes

B b = B.make(); // call factory function

All my uses of RAII depend on copy construction and destruction. Construction is easy enough because I have to explicitly declare it anyway.

-Steve
June 16, 2023

On Friday, 16 June 2023 at 01:00:05 UTC, Steven Schveighoffer wrote:

>

B b = B.make(); // call factory function

-Steve

Thank you for your tip.
If could simplify it a bit more, it would be even better. It's really uncomfortable without this().

« First   ‹ Prev
1 2