Interview written Test (c + + section)

Source: Internet
Author: User

The difference between 1.define and Const,inline

Disadvantages of define:

1. Boundary effect (must be bracketed to avoid boundary effects)

#define MUL (A, b) a*b while in use, such a call:int a=1, b=2, c=3, d=0;d= MUL (A + b,c) is compiled, it becomes D=a+b*C instead of what we want d= (a+b) *c[Solution] Its solution is also simple, is to give each component, the parentheses, You can avoid this type of problem, which is defined when the macro is defined:#define MUL (A, B) ((A) * (c))

2. Type checking is not possible

Define defined variables, is the variable of the Compile-time period , the system at compile time, it is all replaced, but not its variable type property checks, relatively not very safe, there may be potential problems, but not found.

Because it is simply a compile-time replacement, the variable it defines is not allocated memory at runtime and does not occupy memory space.

The variable defined by const is a variable of the run-time period, and if the type does not match, the system will find and prompt or error when running, corresponding, the const variable is a variable at runtime, and the system allocates memory for it.

3. Define substitution before editing, resulting in code difficult to read and maintain

4, the operation results of the non-qualitative:

#define Max (      a) > (b) ( A):(B))int  a=5, b=; Max (++a,b);    // a++ two times Max (++a,b+);   // a++ One time

The difference between define and inline:

Inline is an inline function for function expansion without the need for a function call process, with type checking

Features that are unique to define and cannot be overridden by inline and const:

Header files for # include and #ifdef and #ifndef are repeatedly included

The disadvantages of the header file duplication include: 1, some header file duplication contains only increased the workload of the compilation work, will not cause too much problem, just a little less efficient compilation

2. Some header files are duplicated, which can cause errors, such as defining global variables in the header file (although this method is not recommended, but it is true that the C specification allows)

In general, we write function declarations, class definitions, template definitions, and so on to a header file, the corresponding header file to be used in the #include包含到源文件 (*.cpp file). However, it is possible to include other header files in the header file, which will inevitably occur when a header file is repeatedly included. We can use the compile preprocessing command to avoid this situation.

For example, if you want to make sure that the header file max.h not be duplicated, you can take the following form:

The first preprocessing command is to say that if Maxmin_h is not true, this file is not included, the source code behind this command is valid (equivalent to: ' If the gate is not closed, please come in ');

The second preprocessing command puts the Maxmin_h to True (the equivalent of locking the door and not letting the second person come in).

The last preprocessing command is to mark the scope of the source program that accepts the above processing (which is equivalent to having reached the back door).

However, even with #ifdef #endif, you must remember that the preprocessor still reads the entire header file, even if all the contents of the header file are ignored. Because this processing will slow down the compilation speed, you should avoid multiple inclusions if possible.

Interview written Test (c + + section)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.