Midterm Prep
Created Sunday 19 June 2016
template<typename T>
T adder(T v) {
return v;
}
template<typename T, typename... Args>
T adder(T first, Args... args) {
return first + adder(args...);
}
IMPORTANT NOTES
----------
B *b = static_cast<B*>(new A); is A not B! (can execute A's stuff but not B's UNLESS special case(virtual))
----------
casts have to be either pointers OR references
----------
A B C
A has a virtual function, B and C do not. THEY ALL HAVE VPTRS
----------
// the destructor does not free the memory multiple times.
other.mData = NULL;
other.mLength = 0;
----------
http://accu.org/index.php/journals/268
----------
STUFF TO RESEARCH
- Function Overload Resolution
- specialized templates for functions and classes
- Which headers to use for which functions
- std::forward -> <utility>
- <cstring>
- std::remove_reference -> <type_traits>
- std::swap -> <algorithm>
- std::copy -> <algorithm>
- try, catch, throw
http://thbecker.net/articles/rvalue_references/section_07.html
http://thbecker.net/articles/rvalue_references/section_08.html
CS225 Midterm Checklist - what you are could be tested on (This is list is neither exhaustive nor comprehensive but indicative, that is, you can get the sense of the emphases of the midterm from this list, but this list doesn’t cover everything (e.g., you must know all the C stuff such as pointers and arrays, but we don’t include it here.), moreover, it may also contain stuff that ultimately is not tested in the midterm.
1. General problem solving and programming. You should be able to code stuff that can solve simple problems such as finding substrings etc. So you need to be able to write programs.
2. C++ syntax/semantics/features that you are expected to know:
a. Command-line arguments – this is basic stuff.
Try the following program:
1 #include <iostream> 2 3 int main(int argc, char** argv) { 4 std::cout << "Have " << argc << " arguments:" << std::endl; 5 for (int i = 0; i < argc; ++i) { 6 std::cout << argv[i] << std::endl; 7 } 8 }
Running it with ./test a1 b2 c3 will output
1 Have 4 arguments: 2 ./test 3 a1 4 b2 5 c3
argc is number of arguments (first included), argv is the actual arguments. normally the first argument has the "./" too.
- === Top-level vs low-level consts and the implications (how they affect copy assignments mainly and misc stuff) ===
Const objects can only run const functions - apart from constructor and destructor
JUST THINK ABOUT TYPES
- === Complex C/C++ declarations ===
Should be fine.
- === Data-alignment for Structs (basic stuff) ===
i. C++ -style casts – static_cast, const_cast, reinterpret_cast, dynamic_cast and their appropriate usage.
Inheritance changes the game because now there are three different things we might mean by a pointer cast. Say class D derives from class B. What should (D*)pb do, where pb has type B*? Here are three possibilities:
- (REINTERPRET_CAST) Return a pointer to the same byte of memory, but just change the type of the pointer. (This is the same as what all pointer casts do in C, as explained above.)
- (DYNAMIC_CAST) Check whether the B* really points to a B that is part of a D object. If so, return a pointer to the D object. If not, fail (maybe by returning a null pointer or throwing an exception.)
- (STATIC_CAST)Assume that the B* points to a B that is part of a D object; don't bother performing a check. Adjust the address of the pointer if necessary so that it will point to the D object.
It is conceivable that in C++ we might want to perform any of these three conversions. Therefore C++ gives us three different casts:reinterpret_cast<D*> for function (1), dynamic_cast<D*> for function (2), and static_cast<D*> for function (3).