'Helper' functions in C++

前端 未结 7 784
轻奢々
轻奢々 2021-02-01 03:20

While refactoring some old code I have stripped out a number of public methods that should actually of been statics as they a) don\'t operate on any member data or call any othe

7条回答
  •  心在旅途
    2021-02-01 04:07

    A case where one might use class (or struct) over namespace is when one needs a type, for example:

    struct C {
      static int f() { return 33; }
    };
    
    namespace N {
      int f() { return 9; }
    }
    
    template
    int foo() {
      return T::f();
    }
    
    int main() {
      int ret = foo();
    //ret += foo(); // compile error: N is a namespace
      return ret;
    }
    

提交回复
热议问题