Separate Compilation Flashcards
In general what is seperate compilation in C++?
When one file needs to refer to another (e.g. LinkedList needs a NOde), what do we use to ensure that we can do this?
Specifically how do you use a guard when including items. What does it do?
What would intAtom.cpp look like for the following:
In general, how should you approach includes for both .cpp and .h files?
Why don’t guards solve all our include problems?
Every .cpp file is compuled separately into an object file and THEN they are linked together.
- Guards only stop you from including something more than once in ONE file compilation (e.g. if you included ListItem and Node, and Node was including ListItem too)
- Guards DON’T stop two seperate instance existing in two different translation units (i.e. the contents of a single source file, plus the contents of any header files directly or indirectly included by it)
- e.g. if you included ListItem and so did some other file compiled seperately
- Inclusion of two header files in two seperate spots doesn’t caus ea problem if there’s no executable code - it’s only being brought in so the ompiler can get enough context to compile the class it is working on.
- BUT if there’s any executable code(e.g. a method that is not inline*, a variable initialization…) you’ll get two copies of executable code in your compilation if you do two includes of the header.
- The link editor will then produce errors because it doesn’t ahve a unique place to link to.
- *an inline method is exempt from the “One Definition Rule” (ODR)
What is the solution to the include problem in C++?
For the attached code (which represents GenericList.h):
- Why do we need to include ListItem.h
- Why don’t we need to include Node.h?
In C++ what is a forward reference and how do you make one?
In the attached code (in GenericList.h):
Why does class Node work?
What are good general rules for including/forward referencing?
When should you use #include?
When are other instanaces would you use forward references
In the included code, why use a forward reference to C2.
In the attached code, do we need the forward reference class ListItem. Why/Why not?