Include .cpp instead of header(.h)

sysop picture sysop · Feb 13, 2012 · Viewed 27.8k times · Source

There are some cases when we include .cpp file instead of standard header file (.h), for example:

#include "example.cpp"

instead of

#include "example.h"

It seems to work but is this safe or should I avoid it?

What about the compilation time?

Answer

zellio picture zellio · Feb 13, 2012

It's lazy coding. Use header files. Yes they can increase compile time but they mean that you can easily re-implement chunks of your code, or better yet, another developer could at anytime. The header file serves as a template for what your C/C++ code is going to do. It's a bad idea to discard or ignore it.