A brief talk about the differences between ා import, ා include and @ class in C / C + +

Time:2020-9-21

1. Generally speaking, “import” is used to import the header file of objective C, and “include” is used when C / C + + header file is included.

2. Import determines that a file can only be imported once, so that you will not have problems in recursive inclusion. < mark >

Therefore, the advantage of “import” over “include” is that it will not cause cross compilation.

#import && #class:

1. Import will contain all information about this class, including entity variables and methods. However, @ class only tells the compiler that the names declared after it are the names of the classes. As for how these classes are defined, I will tell you later.

2. In the header file, you only need to know the name of the referenced class. You don’t need to know the internal entity variables and methods, so you usually use @ class in the header file to declare that the name is the name of the class. In the implementation class, because the internal entity variables and methods of the reference class will be used, you need to use ා import to contain the header file of the referenced class.

Note: ා import is to go through the header file of the referenced class, that is, the variables and methods in the. H file are included once and only once, while @ class is not used, so the latter is more efficient.

3. In terms of compilation efficiency, if you have 100 header files that import the same header file, or these files are referenced in turn, such as a – > b, B – > C, C – > D. When the initial header file changes, all subsequent classes that reference it need to be recompiled. If you have many classes, this will take a lot of time. You can’t use @ class.

4. If there is a circular dependency, such as a – > b, B – > A, if you use ා import to include each other, then there will be compilation errors. If @ class is used to declare each other in the header files of the two classes, there will be no compilation errors.

Note: it has been proved that a and B import each other without compiling errors. Since it has been stated in the < mark >, the file will be imported only once when ා, so this article does not hold.

Summary:

1. If it is not C / C + +, try to use “import”.

2. If you can import in the implementation file, you will not import in the header file.

3. If you can import in the header file @ class +, you will not import in the header file.