lin91957
Cholerny Spammer
Joined: 22 Jul 2010
Posts: 461
Read: 0 topics
Location: xgnddr
|
Posted: Fri 17:56, 08 Apr 2011 Post subject: herve leger toronto Analysis of C + +, inline func |
|
|
Analysis of C + +, inline functions and macros difference
+ 1) Although the trivial example above but the problem is obvious. Macro definition is prone to ambiguities. ... Use the expression in the macro call as a parameter. Problems have emerged. The first issue is launched within the macro expression, they differ from the priority of the priority we expect for example: # defineOO (x) (X * X) we use a number to call it, DD (10), so Nothing seems wrong, the results returned 100, is correct, but if we use DD (10 +10) to call, we expect the result is 400, and the results of the macro call is (10 +10 X10 +10), the results 120, this is clearly not the result we want the method to avoid these errors is added to the macro parameters are in parentheses: # defineDD (x) ((x) ten (x)) This will ensure that no mistake, however, Even with this definition, the macro still can go wrong,[link widoczny dla zalogowanych], this is the second question, the problem is more subtle. Unlike a normal function, each time using a parameter in the macro, all of the parameters evaluated. As long as the macro call only normal variables, the evaluation began. However, if the parameters evaluated side effects, then the result may be unexpected,[link widoczny dla zalogowanych], well certainly does not mimic the function behavior. For example, using the DD (a + +) calls it, they purported to want (a +1) · (a +1) results,[link widoczny dla zalogowanych], whereas in reality? We can look at the results of macro expansion: (a ++)×( a + +), If a value is 4,[link widoczny dla zalogowanych], we get the result is 5 × 6 = 30. And we expect the result is 5 × 5 = 25. Is this a problem. In fact, in some c library functions have these problems. For example: Toupper (pChar + +) will perform twice on the pChar + + operator, as Toupper actually a macro. In addition, the macro can not access the object's private members are clear that this is not what we want to look like a function call in the desired macro. In this case, the obvious solution is to design an effective real function. For example: inlineintDD (intx) {returnX * XI} inty-DD (i + +) I compiler generates the following code: intyIi + + ty = i × iI In addition, inline functions to access the object's private members of the inline function in C + + classes the most widely 【should be used to define the access function. We define the class data members will generally be defined as private or protected, so that outsiders can not directly read the data members of our class. For private or protected members of the members to read and write interface functions must be used to carry out. If we define the read and write member functions inline function, it will get better efficiency. Classsample {Private: IntnTestlPublic: IntreadtestO {returnnTestI} Voidsettest (intI) {nTest = I}}} Of course,[link widoczny dla zalogowanych], an inline function has some limitations. Is a function of the code can not be too much, if, inline function body of the function is too large, the compiler will give a general way within the Union, while the ordinary way of calling a function. This inline function and ordinary functions on the efficiency of the same.
More articles related to topics:
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
The post has been approved 0 times
|
|