This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
programming:cplusplus:start [2019/10/19 20:14] dwheele [Difference between Dot and Arrow] |
programming:cplusplus:start [2022/02/28 18:43] (current) dwheele [Reference] |
||
|---|---|---|---|
| Line 2: | Line 2: | ||
| Helpful tutorials on YouTube are by TheChernoProject https:// | Helpful tutorials on YouTube are by TheChernoProject https:// | ||
| + | |||
| + | (Too much of a beginner) Trying course from https:// | ||
| + | |||
| + | |||
| + | ===== include ===== | ||
| + | |||
| + | Includes library of code, either from local (name is in double-quotes) or in the language-provided libraries (name is in angle-brackets). | ||
| + | |||
| + | <code c++> | ||
| + | #include < | ||
| + | </ | ||
| + | |||
| + | Use angle brackets for built-in libraries. | ||
| + | |||
| + | ===== using namespace ===== | ||
| + | |||
| + | <code c++> | ||
| + | using namespace std; | ||
| + | </ | ||
| + | |||
| + | Places this code into the std namespace. That way, we can use '' | ||
| + | |||
| + | ===== Linker ===== | ||
| + | |||
| + | The C++ compiler creates and Object file, but separately. The Linker puts them together. | ||
| + | |||
| + | ===== Pointers ===== | ||
| + | |||
| + | https:// | ||
| + | |||
| + | A pointer is an integer which stores a memory address. Types have nothing to do with that. | ||
| + | |||
| + | |||
| + | The value 0 is not a valid memory address. It means 0 | ||
| + | |||
| + | <code c++> | ||
| + | int main() | ||
| + | { | ||
| + | // void* ptr = 0; // or void* ptr = nullptr; | ||
| + | int var = 8; | ||
| + | // Where are you in memory | ||
| + | int* ptr = &var; // Get the location of var, and put it into ptr as an integer pointer. | ||
| + | |||
| + | *ptr = 9; // int value pointed at by pointer " | ||
| + | |||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code c++> | ||
| + | int main() | ||
| + | { | ||
| + | char* buffer = new char[8];// This uses heap | ||
| + | | ||
| + | |||
| + | | ||
| + | |||
| + | | ||
| + | |||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Reference ===== | ||
| + | |||
| + | Pointers and references are about the same thing. How we write them is different. References are syntax sugar on top of a pointer. | ||
| + | |||
| + | References reference an existing variable. | ||
| + | |||
| + | <code c++> | ||
| + | int main() | ||
| + | { | ||
| + | int a =5; | ||
| + | | ||
| + | // We have created an alias. It is not really a variable. | ||
| + | // We can use ref as though it is " | ||
| + | |||
| + | ref = 2; | ||
| + | | ||
| + | } | ||
| + | // Works, but this is clumsy | ||
| + | |||
| + | void Increment (int* value) | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | int main() | ||
| + | { | ||
| + | // We want to pass the memory address. | ||
| + | int a = 5; | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | <code c++> | ||
| + | // Use a reference instead of a pointer | ||
| + | |||
| + | void Increment(int& | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | int main() | ||
| + | { | ||
| + | int a = 5; | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| ===== Memory ===== | ===== Memory ===== | ||
| Line 50: | Line 162: | ||
| To access members of a structure, use the dot operator. To access members of a structure through a pointer, use the arrow operator. [[https:// | To access members of a structure, use the dot operator. To access members of a structure through a pointer, use the arrow operator. [[https:// | ||
| + | ====== Troubleshooting while Building ====== | ||
| + | |||
| + | Sometimes the *.h.gch will get corrupted or contain outdated information, | ||