Get the App
SLTechnology News&Howtos  ›  Development  › 

How to migrate c to d0

Shulou Source: shulou.com Published: 2022-06-03 05:43:08 09月20日 Update

This article mainly explains "how to transplant c to d0". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "how to transplant c to d0".

Convert enumerations only need to copy / paste.

/ / In C

Enum

{

ME_FOO

ME_BAR

ME_BAZ

}

/ / In D

Enum

{

ME_FOO

ME_BAR

ME_BAZ

}

Note that there is no semicolon in d and there can be a comma. Name the enumeration with a prefix.

/ / In C

Typedef enum

{

ME_FOO

ME_BAR

ME_BAZ

} MyEnum

/ / In D

Enum MyEnum

{

ME_FOO

ME_BAR

ME_BAZ

}

/ / in the function

MyEnum me = MyEnum.ME_FOO;// here

Mainly for type safety, convenience and compatibility:

Alias MyEnum.ME_FOO ME_FOO

Alias MyEnum.ME_BAR ME_BAR

Alias MyEnum.ME_BAZ ME_BAZ

/ / add an alias

MyEnum me = ME_FOO

But the above is too redundant, if security is not important, you can do this:

Alias int MyEnum

Enum

{

ME_FOO

ME_BAR

ME_BAZ

}

This is the same as c's behavior.

# define,c is used to define constants. Enumerations in d can represent not only traditional enumerations, but also list constants, so there is no need to use invariant qualifiers. List constants are enumerations that contain only one member, so there is no need for parentheses.

/ / floating list constant

Enum float Foo = 1.003f

/ / declare with a dynamic reference that the mei can automatically derive the type.

Enum Foo = 1.003f; / / float

Enum Bar = 1.003; / / double

Enum Baz = "Baz!" / / string

/ / the following example:

/ / C terminal

# define FOO_SOME_NUMBER 100

# define FOO_A_RELATED_NUMBER 200

# define FOO_ANOTHER_RELATED_NUMBER 201

/ / D side

Enum FOO_SOME_NUMBER = 100

Enum FOO_A_RELATED_NUMBER = 200

Enum FOO_ANOTHER_NUMBER = 201

/ / grouped into a group

Enum

{

FOO_SOME_NUMBER = 100

FOO_A_RELATED_NUMBER = 200

FOO_ANOTHER_NUMBER = 201

}

If it is too much, group it into one group, and do it alone if you have two players. There can be more than one string.

/ / In C

# define LIBNAME "c library"

# define AUTHOR "XX"

# define COMPANY ""

/ / D, all can be added to the string

Enum: string

{

LIBNAME = "c library"

AUTHOR = "XX"

COMPANY = ""

}

Of course, Mei: (inheriting) other structures are also possible. Pay attention to the tail comma.

Structure, most c structure, can be slightly / even without modification. Change it directly to d

/ / In C

Struct foo_s

{

Int x, y

}

Typedef struct

{

Float x

Float y

} bar_t

/ / In D

Struct foo_s

{

Int x, y

}

/ / No semicolon.

Struct bar_t

{

Float x

Float y

}

It is mainly typedef. Sometimes there are two structural names.

Thank you for your reading. the above is the content of "how to transplant c to d0". After the study of this article, I believe you have a deeper understanding of how to transplant c to d0. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Migration constants lists learning security content semicolons that is types commas important no two Chinese traditional redundancy functions aliases prefixes names Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Docker NVidia Apple