I forgot when to start, and my mind had this idea:
1. Local variables should be defined inCodeStarting Point
2. The construction of local variables is performed when you enter the function. Its time is irrelevant to the position declared by the local variables.
I remember that this idea should come from authoritative books or some rich experience. In my opinion, it is like the sacred development of the magicians who hold the staff and wear the Chinese costumes in the eyes of mortals. This point of view was so authoritative in my opinion at the time that I was not questioned during the university.
Due to this feature, I have always had a question: In raiI, in order to minimize the number of critical sections, it is not necessary to make a deliberate function split for this purpose, extract the critical section and wrap it with a separate function.
However, when I recently read the log4cplus code, I see the following code snippet:
Do{: Log4cplus: thread: Guard _ sync_guard_object (mutex );This-> Errorhandler = eh ;}While(0)
This Code raises a question about my long-term point of view. If that is correct, why do we use the do loop to create a subdomain? So I wrote some code to test it:
# Include "Stdafx. H" # Include <Iostream> Using namespace STD ; # Define Mainsizeof main Class OBJ { Public : OBJ (){ Cout < "OBJ construct ..." < Endl ;}~ OBJ (){Cout < "OBJ destruct ..." < Endl ;}}; Void Localvarinitest1 (){ Cout < "Local variable initial test function 1: do {}" < Endl ; Int I = 2; Do { Cout <"Begin of the loop body ..." < Endl ; OBJ o ; Cout < "End of the loop body ..." < Endl ;} While (-- I ); Cout < "End of function body 1" < Endl < Endl ;}Void Localvarinitest2 (){ Cout < "Local variable initial test function 2 :{}" < Endl ;{ Cout < "Begin of the loop body ..." < Endl ; OBJ o ; Cout < "End of the loop body ..." < Endl ;} Cout < "End of function body 2" < Endl < Endl ;} Void Localvarinitest3 (){ Cout < "Local variable initial test function 3: If {}" < Endl ; If ( True ){ Cout <"Begin of the loop body ..." < Endl ; OBJ o ; Cout < "End of the loop body ..." < Endl ;} Cout < "End of function body 3" < Endl < Endl ;} Void Localvarinitest4 (){Cout < "Local variable initial test function 4: No sub range" < Endl ; OBJ o ; Cout < "End of function body 4" < Endl < Endl ;} Void Localvarinitest5 (){ OBJ o ; Cout <"Local variable initial test function 5: No sub range" < Endl ; Cout < "End of function body 5" < Endl < Endl ;} Int Mainlocalvarini (){ Localvarinitest1 (); Localvarinitest2 (); Localvarinitest3 (); Localvarinitest4 (); Localvarinitest5 (); Getchar (); Return 1 ;}
This sectionProgramThe output on vc6 is as follows:
Local variable initial test function 1: do {}
Begin of the loop body...
OBJ construct...
End of the loop body...
OBJ destruct...
Begin of the loop body...
OBJ construct...
End of the loop body...
OBJ destruct...
End of function body 1
Local variable initial test function 2 :{}
Begin of the loop body...
OBJ construct...
End of the loop body...
OBJ destruct...
End of function Body 2
Local variable initial test function 3: If {}
Begin of the loop body...
OBJ construct...
End of the loop body...
OBJ destruct...
End of function body 3
Local variable initial test function 4: No sub range
OBJ construct...
End of function body 4
OBJ destruct...
OBJ construct...
Local variable initial test function 5: No sub range
End of function body 5
OBJ destruct...
Some conclusions can be drawn from the output:
1. The object construction time depends on its definition location. The initialization process will not be delayed or delayed by the compiler.
2. The object's destructor is automatically executed by the compiler at the end of the life cycle (exit the definition domain.
3. the variables defined in the loop body are initialized and destructed multiple times.
4. The domain is defined as {}. It can be a function body, do, while, if, or other compound statements. A separate {} can also define a subdomain.
However, this is not all. What I mentioned earlier should not be a hole or an illusion. So I looked for it and found that it actually came from C. in C language, all local variables must be defined at the starting position of the function body. Create a. c file and add the following code. The file fails to be compiled.
VoidFun (){IntA; A = 1;IntB; A = 2; B = 2 ;}
There seems to be some related things that cannot be found at the moment and will be added later.