The C ++ programming language supports various programming styles and supports various functions in the C language. How should we correctly apply such a powerful programming language to program development? First, let's start with some basic knowledge, such as some concepts of C ++ sizeof introduced today.
- Detailed description of C ++ address Operators
- Interpretation of c ++ function transmission in three ways
- Function implementation of C ++ clock ()
- Explanation of the specific code application of C ++ timer
- Application details analysis of c ++ standard library
1. What is sizeof
First, let's take a look at the definition of sizeof on msdn:
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.
When I saw the word "return", did I think of a function? Wrong. sizeof is not a function. Have you ever passed parameters to a function without brackets? Sizeof is acceptable, so sizeof is not a function. Some people on the Internet say that sizeof is a unary operator, but I don't think so, because C ++ sizeof is more like a special macro, Which is evaluated during the compilation stage. For example:
- Cout <sizeof (int) <endl; // the length of an int on a 32-bit machine is 4.
- Cout <sizeof (1 = 2) <endl;
// = The operator returns the bool type, which is equivalent to cout <sizeof (bool) <endl;
It has been translated:
- cout<<4<<endl;
- cout<<1<<endl;
Here is a trap. Let's look at the following program:
- int a = 0;
- cout<<sizeof(a=3)<<endl;
- cout<<a<<endl;
Why is the output 4 or 0 instead of the expected 4 or 3 ??? It lies in the features of C ++ sizeof in the compilation phase. Because sizeof cannot be compiled into machine code, the content in the scope of sizeof, that is, (), cannot be compiled, but replaced with the type. = Operator returns the type of the left operand, so a = 3 is equivalent to int, and the code is replaced:
- int a = 0;
- cout<<4<<endl;
- cout<<a<<endl;
Therefore, sizeof cannot support chained expressions, which is different from the unary operator.
Conclusion: Do not treat sizeof as a function, or a mona1 operator, and treat it as a special compilation preprocessing.
2. sizeof usage
C ++ sizeof has two usage methods:
1) sizeof (object)
That is, the sizeof object can also be written as a sizeof object. For example:
2) sizeof (typename)
That is to say, sizeof is used for the type. In this case, it is invalid to write sizeof typename. The following are examples:
- Int I = 2;
- Cout <sizeof (I) <endl; // sizeof (object) usage, reasonable
- Cout <sizeof I <endl; // sizeof object usage, reasonable
- Cout <sizeof 2 <endl; // 2 is parsed to an int-type object. The usage of sizeof object is reasonable.
- Cout <sizeof (2) <endl; // 2 is resolved to an int-type object. The usage of sizeof (object) is reasonable.
- Cout <sizeof (int) <endl; // sizeof (typename) usage, reasonable
- Cout <sizeof int <endl; // error! For operators, you must add ()
We can see that adding () is always the right choice.
Conclusion: It is best to add () regardless of the value of C ++ sizeof ().