In c++, is there any other way, besides header files, to use a function defined in file A.cpp, inside file B.cpp that would be considered good programming practice?
Asked
Active
Viewed 2,138 times
1
-
compiler extension that allows symbolic includes... – ratchet freak Mar 24 '14 at 19:33
-
@gnat, that question did not list alternatives to header files. – user2738698 Mar 24 '14 at 19:33
-
http://meta.stackoverflow.com/tags/list-questions/info – gnat Mar 24 '14 at 19:35
-
1It does not ask for a list of answers; it merely asks for "a(as in one) DRY alternative to c++ header files" – user2738698 Mar 24 '14 at 19:41
2 Answers
6
With your restriction of "besides header files", the answer is: No.
The C++ compiler compiles each source file independently. If you intend to use a declaration that appears only once, it must appear in a header file.
(This does not consider things that wouldn't be considered good programming practice, such as including one .cpp file within another, or using -D compiler command line macros to define extern symbols in more than one source file.)
Greg Hewgill
- 10,231
4
Lazy C++ can automate the generation of .h and .cpp files from a common .cpp-like source, so you don't have to repeat yourself by maintaining the header file yourself.
Josh Kelley
- 11,051
- 7
- 39
- 50