Template definitions outside class body

后端 未结 3 1027
梦如初夏
梦如初夏 2021-01-01 19:08

Is it O.K. to define virtual function of class template outside its body? Virtual function can not be inlined, but to avoid multiple definitions in compilation units they sh

3条回答
  •  有刺的猬
    2021-01-01 19:35

    You can define the functions there, as long as any code which needs to instantiate the function in question has visibility of that code at compile time (not link time).

    It's quite common to separate a template into 2 files, one being a traditional header, and the second being the implementation, as with non-templated functions and their implementation. The only difference is that you need to #include the template implementation file as well as the header when you want to use it.

提交回复
热议问题