Last active
February 15, 2024 16:52
-
-
Save Offirmo/6cf8d8d7b754d1192b441458777bdb4b to your computer and use it in GitHub Desktop.
[Basic C++]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://isocpp.org/images/uploads/2-Tour-Basics.pdf | |
std::cout << __func__ << __FILE__ << __LINE__ << std::endl; | |
// XXX scalar vs aggregate | |
const auto foo { 42 }; // valid but {} hints at aggregate (in theory) | |
const auto foo = 42; | |
const auto str { "foo" }; | |
struct Ruler { | |
std::string name {}; | |
int reign_begin {}; | |
int reign_end {}; | |
}; | |
// MEMORY MANAGEMENT | |
// https://isocpp.org/wiki/faq/freestore-mgmt | |
// - if you need an auto-free in scope, use the stack!! | |
const Ruler from_stack{ .name{"Clovis I"}, .reign_begin=509, .reign_end=511}; // https://en.cppreference.com/w/cpp/language/aggregate_initialization | |
// - make_unique (or make_shared) are nearly always superior to both new/malloc() and completely eliminate delete/free() | |
auto from_unique = std::make_unique<Ruler>(Ruler{ .title{"king"}, .name{"Clovis I"}, .reign_begin=509, .reign_end=511}); | |
*from_unique.get(); | |
// - new -> delete | |
const Ruler* from_new = new Ruler{ .name{"Clovis I"}, .reign_begin=509, .reign_end=511}; | |
delete from_new; from_new = nullptr; | |
// - malloc() -> free() | |
// https://stackoverflow.com/a/67967472/587407 | |
int* x = new int[5]; // gv gv gv gv gv (gv - garbage value) | |
int* x = new int[5](); // 0 0 0 0 0 | |
int* x = new int[5]{}; // 0 0 0 0 0 (Modern C++) | |
int* x = new int[5]{1,2,3}; // 1 2 3 0 0 (Modern C++) | |
char* p = new (std::nothrow) char [1048576]; | |
)if (!p) { /*...*/ } // null pointers are implicitly converted to false | |
delete p; p = nullptr; | |
//////////// | |
enum class Color { red, blue, green }; // enum class hides the "enum" | |
Color col = Color::red; // no need to mention "enum" 👍 | |
Color next = static_cast<Color>((static_cast<int>(col) + 1) % 3); // DANGER | |
//////////// | |
switch (answer) { | |
case ’y’: | |
return true; | |
case ’n’: | |
return false; | |
default: | |
cout << "Sorry, I don’t understand that.\n"; | |
++tries; // increment | |
} | |
//////////// | |
// lambdas | |
// https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp | |
// XXX mutable by copy!! https://stackoverflow.com/a/43129100/587407 | |
//////////// | |
// tuples | |
// exceptions | |
// https://learn.microsoft.com/en-us/cpp/cpp/errors-and-exception-handling-modern-cpp | |
#include <exception>; | |
throw std::invalid_argument("MyFunc argument too large."); | |
try | |
{ | |
//... | |
} | |
catch (std::exception& e) | |
{ | |
std::cerr << "exception caught: " << e.what() << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment