How to implement static class member functions in *.cpp file?

前端 未结 8 1475
Happy的楠姐
Happy的楠姐 2020-12-07 15:35

Is it possible to implement static class member functions in *.cpp file instead of doing it in the header file ?

Are all static functions a

8条回答
  •  天命终不由人
    2020-12-07 15:55

    helper.hxx

    class helper
    {
     public: 
       static void fn1 () 
       { /* defined in header itself */ }
    
       /* fn2 defined in src file helper.cxx */
       static void fn2(); 
    };
    

    helper.cxx

    #include "helper.hxx"
    void helper::fn2()
    {
      /* fn2 defined in helper.cxx */
      /* do something */
    }
    

    A.cxx

    #include "helper.hxx"
    A::foo() {
      helper::fn1(); 
      helper::fn2();
    }
    

    To know more about how c++ handles static functions visit: Are static member functions in c++ copied in multiple translation units?

提交回复
热议问题