In which file do we put non-member function in C++?

前端 未结 3 779
广开言路
广开言路 2021-01-28 15:49

What is the normal practice when it comes to non-member function in C++? Do we put them in main.cpp or header file or class implementation file, or do we make a separate .cpp fi

3条回答
  •  悲&欢浪女
    2021-01-28 16:14

    I would say you should not treat non-member functions differently to classes and member functions and other symbols.

    You should create a distinct header file .h and a corresponding source file .cpp for each logical component (module) of your application.

    All public symbols should be declared/defined in the header file (whether they be non-member functions or otherwise) and all non-public symbols and all required definitions should go in the source file.

    In short, group according to logical program components, rather than by the type of symbol/function.

提交回复
热议问题