繁体   English   中英

什么是好习惯? 仅包含在头文件中,还是包含在头文件和源文件中? [关闭]

[英]What is a good practice? Include only in header file or maybe in both header and source? [closed]

在这种情况下你建议做什么,例如你需要在头文件和源文件中使用<algorithm>中的函数?

example.hpp:

#include <algorithm>
#include <vector>

class Example
{
public:
  void definitionInHeader() { std::fill(m_values.begin(), m_values.end(), 10); }
  void definitionInSource();
private:
  std::vector<int> m_values(10);
}

example.cpp:

#include "example.hpp"

void Example::definitionInSource()
{
  //Code from <algorithm> is available here due to inclusion of example.hpp
  std::vector<int> integers{ 1, 2, 3, 4, 5 };
  auto print = [](int i){ std::cout << i << std::endl };
  std::for_each(integers.begin(), integers.end(), print);
}

您是否建议明确包含在源文件中,或者仅包含在头文件中? 在我看来,额外的包含成本是没有意义的,但如果将来有人会重构这样的代码并从头文件中删除,那么源文件也必须进行编辑。 另一方面,源文件中的include部分可能会有点长。 您有什么推荐的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM