Why do functions work in class file when #include <cstring> is in source file?

StackOverflow https://stackoverflow.com/questions/23416538

  •  13-07-2023
  •  | 
  •  

문제

I'm using VS 2013, and Horton C++ 2010.

My chapter exercise, Ex9_01.cpp, has strcpy_s() and strlen() in the class file CandyBox.h and it didn't work until I "#include cstring" in in CandyBox.h.

Then as I read more of the example I saw that "#include cstring" was in the source file, Ex9_01.cpp, so I removed "cstring" from CandyBox.h and example stilled worked!
WHY?
Since the file Candybox.h doesn't "#include Ex9_01.cpp" why does it look at Ex9_01.cpp to know about 'cstring'?

P.S. Sorry about poor formatting, this is my first post at site and I guess I didn't read all the instructions. Will do better next time (am short of time now, running for an airplane).

Astro

도움이 되었습니까?

해결책

The compiler only looks at cpp files. An h file only gets compiled when it is #include'ed in a cpp file, just as if it had been pasted there. So what probably happens in your case is that the cpp file includes cstring.h, then it includes candybox.h. So cstring.h is "seen" before candybox.h.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top