Skip to content

Instantly share code, notes, and snippets.

@caviles
Created June 4, 2017 00:40
Show Gist options
  • Save caviles/fa24f1c4742613ded3f0a61e7e3e9965 to your computer and use it in GitHub Desktop.
Save caviles/fa24f1c4742613ded3f0a61e7e3e9965 to your computer and use it in GitHub Desktop.
C++ templates are set at compile time not runtime like C#, java
//returns max of two values
template <class T>
T max(T& t1, T& t2){
return t1 < t2 ? t2 : t1;
}
overload operators in c++ often
instead creating a Add() overload + and or +=
in a class f you don't specify the default is private in a struct the default is public
C++ style guide
https://google.github.io/styleguide/cppguide.html
enum e_acomany {
Audi,
BMW,
Cadillac,
Ford,
Jaguar,
Lexus
Maybach,
RollsRoyce,
Saab
};
e_acompany my_car_brand;
my_car_brand = RollsRoyce; // notice you don't do e_acomany. so decorate the enum name around the member
if (my_car_brand == Ford)
cout << "Hello, Ford-car owner!" << endl;
@caviles
Copy link
Author

caviles commented Jun 4, 2017

//bitwise
cout << (5 & 3) << '\n'; //1
cout << (1 & 0) << '\n';//0
cout << (0 ^ 1) << '\n';//1
//shift bits
int i =4;

cout << (i >> 1) << '\n'; //2 moves 1 bit to the right
cout << (i << 1) << '\n'; //8 moves 1 bit to the left

@caviles
Copy link
Author

caviles commented Jun 4, 2017

friend gives member function access to share a private member

@caviles
Copy link
Author

caviles commented Jun 4, 2017

A pointer is a variable that holds the address to another variable. int* a = &p; // *a is = to memory location of p; *a = 5; The reference is now updated.

@caviles
Copy link
Author

caviles commented Jun 4, 2017

const promises that it will not change members, that means const fn can only call other const fns

@caviles
Copy link
Author

caviles commented Jun 4, 2017

declare a pointer to be a const int* const ptr or to point to a const // const int* ptr =&a;

@caviles
Copy link
Author

caviles commented Jun 5, 2017

cons of manual memory management
-delete twice - error
-never delete - memory leak

  • delete to soon - app blows up when someone tries to use it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment