Why have header files and .cpp files? [closed]

2018-12-31 01:26发布

Why does C++ have header files and .cpp files?

9条回答
梦醉为红颜
2楼-- · 2018-12-31 02:21

Because C, where the concept originated, is 30 years old, and back then, it was the only viable way to link together code from multiple files.

Today, it's an awful hack which totally destroys compilation time in C++, causes countless needless dependencies (because class definitions in a header file expose too much information about the implementation), and so on.

查看更多
不流泪的眼
3楼-- · 2018-12-31 02:23

Because C++ inherited them from C. Unfortunately.

查看更多
刘海飞了
4楼-- · 2018-12-31 02:28

It's the preprocessor way of declaring interfaces. You put the interface (method declarations) into the header file, and the implementation into the cpp. Applications using your library only need to know the interface, which they can access through #include.

查看更多
登录 后发表回答