OOP vs macro problem

前端 未结 7 1216

I came across this problem via a colleague today. He had a design for a front end system which goes like this:

class LWindow
{
   //Interface for common meth         


        
7条回答
  •  再見小時候
    2021-01-04 13:05

    I found that

    Using the preprocessor #define directive to define constants is not as precise.

    [src]

    Macros are apparently not as precise, I did not even know that... The classic hidden dangers of the preprocessor like:

    #define PI_PLUS_ONE (3.14 + 1)`
    

    By doing so, you avoid the possibility that an order of operations issue will destroy the meaning of your constant:

    x = PI_PLUS_ONE * 5;`
    

    Without parentheses, the above would be converted to

    x = 3.14 + 1 * 5;
    

    [src]

提交回复
热议问题