如题,谢谢!

解决方案 »

  1.   

    有副作用的函数 normally means the 函数 does something to the environment, sometimes without the caller knowing it, say, you have a global variable;int g_value = 1;class yourclass
    {
     void DoWork()
     {
      //do something
      //then
      //(might need critical section to guard against concurrent uses)
       //or use thread-safe increment function
       g_value++;  //side effect
     } void IncrmentGlobalVariableGValue() 
     { 
       //might need critical section to guard against concurrent uses
       //or use thread-safe increment function
        g_value++;
     }
    }in this case, DoWork() 有副作用, because people are calling your method, if you don't document it (and most of time, the function is in a cpp body file and they don't get to see it), they didn't know it could change a global variable, which is something irrelevant to your class, this could surprise your callerbut IncrmentGlobalVariableGValue() is a little different, because the caller knows what your method is doing