0

If there're three files, let's say main.cpp, a.h and b.h. main.cpp includes a.h and a.h includes b.h, but main.cpp does not include b.h directly. class b is declared in b.h.

May I ask if we can use class b in main.cpp directly right now?

gasoon
  • 775
  • 4
  • 8
  • 14
  • 1
    Yes, as b is included, albeit indirectly. Don't forget that if there are b.cpp and/or c.cpp files you will also have to compile them & link them all together. – Avi Berger Aug 18 '22 at 03:08
  • 3
    It's best to include what you use where you use it. If `a.h` changes and no longer includes `b.h` your program will fail to compile. – Retired Ninja Aug 18 '22 at 03:10
  • If you imagine the pre processor simply removing each #include line and pasting the entire contents of the .h file in at that location, so that it ends up with one giant .cpp file containing the contents of all the headers at the top… you’d be 100% correct. – Jeremy Friesner Aug 18 '22 at 03:13
  • @gasoon `#include`s are transitivie. See dupe: [How does #include work in C++?](https://stackoverflow.com/questions/35720656/how-does-include-work-in-c) – Jason Aug 18 '22 at 04:15

0 Answers0