Understand "Chaos IN C ++ :: is the type? Or is it a function call? "

zhaozj2021-02-16  57

Original and comments see: "Chaos IN C :: is the type? Or is it a function call? "Http://blog.9cbs.net/jinhao/archive/2004/06/29/29021.aspx

After reading the original comments, I found that there were many people misunderstand that "how to declare the cranks". At first, I am also a foggy, but I finally understood it, I will have a little summarizing here, and I have given a wake up.

The author's code I compiled in VC . Net2003. In the case of the C compiler of Microsoft, VC . Net2003 is the closest C standard. Because of this, VC . Net2003 should not ignore its warning when compiling VC . Net2002 and VC 6, because these warnings are often pointed out in VC . Net2003 is different from previous versions (even incompatible) If you are simple to ignore, the behavior of the code may not be what you want. Because it is not preparing to discuss this problem, only give a common example:

Bool Bisok = NFLAGS & 0x02 == 0x02; // Warning C4554: "&": Check the error in the operator priority; use parentheses to clarify priority

When compiling, you should pay attention to this "warning" (I think this is a "error") and change the code to:

BOOL BISOK = (nflags & 0x02) == 0x02;

What is A (a ())?

There are currently two statements:

Function declaration variable declaration

Supporting "(2) Variable Declaration" This view is believed that A () is to call the "default constructor" constructed a Temperature Variable of a type A, and then call A "copy constructor" to construct a Class A-type variable A.

Let's take a look at what the code below said:

A a ();

Is this a variable declaration? No, with the default constructor declare a variable should be written: a a; it should be a function declaration: the function name is A, no parameters, the return value is a type A type.

Let's take a look at A ()? In fact, in different occasions, A () has different significance:

Use: a (); represents: struct a __cdecl (void), this is an anonymous function declaration. Function declaration: Void func (a ()); indicating: void __cdecl * (struct a (__cdecl *) (void)), is clear, here A () has become a function pointer declaration: Struct a (__cdecl * (void). In the expression:? For_each (Arr, Arr 3, A ()); here, A () can be used as an expression, it is clear that only the "default constructor" can comply with this purpose.

What is the current A a (a ()) now? Here, when the function declaration and variable declaration can be interpreted, the function declares priority.

A A (A (), 3), can only be interpreted as a variable declaration (call a general constructor).

A a = a (), can only be interpreted as a variable declaration (call copy constructor).

Where is the meaning of this article?

There is a person who believes that this article does not make sense, even boring, and even think that it is confused. The reason is that the writer must have a good coding style, such as: A (a ()) If it is a variable declaration, it should be written: a a = a (). A a a (a ()) If it is a function declaration, you should write: A A (a (*) ()).

This is true, and the good style is extremely important.

However, many people often make similar mistakes, not because the coding style is not good, but it is precisely caused by the content you want to express. One of the most common mistakes is as follows:

A a ()

Originally, it may be to declare a variable, but it becomes a function declaration. It is because of the habitual plus parentheses. And if you don't realize that this is a function statement, it may not be quite clear.

转载请注明原文地址:https://www.9cbs.com/read-18136.html

New Post(0)