Unit_3: C++ Basics Flashcards
1
Q
What is forward declaration? Why need it? How to use it?
A
- a way to tell the compiler that a type exists without providing its full definition. So use when just need pointer/reference, don’t need to know class size or access class members
- => reduce compilation time (fewer include), allow pointer/reference declare w/o full class definition
- eg: class B; =>just tell compiler B exists, so we can use pointer/reference
2
Q
When to #include?
A
- extending that class (.h) -> know members of superclass
- implementing that class (.cpp): know about its own members
3
Q
What’s memory leak? Types?
A
Memory leak happen when we don’t probably reclaim memory -> unusable for the rest of program
1. declared variables have scope
=> must free pointer’s memory before it goes out of scope.
So in linkedlist, delete only free the immediate node (newNode) but nodes linked to it are not. => need destructor
4
Q
What is #pragma once
A
- used at top of header files only -> this file should be include only once
- only stop you from including something more than once in ONE file compilation -> can’t protect you from circular include (eg include ListItem and Node, and Node was including ListItem too)
5
Q
What happends if you put executable code in header file?
A
get 2 copies of executable code in compilation -> link editor errors.
6
Q
What’s self reference?
A
When a class needs to refer to itself (eg access its own methods/fields; pass a reference to itself) => this