Skip to content

Instantly share code, notes, and snippets.

@c0ldlimit
Last active August 29, 2015 14:16
Show Gist options
  • Save c0ldlimit/cf97521568c97fa0f52e to your computer and use it in GitHub Desktop.
Save c0ldlimit/cf97521568c97fa0f52e to your computer and use it in GitHub Desktop.
#lecture9
void
Stack::push(int val)
{
StackHead expected = head.load();
StackItem *newItem = new StackItem(val);
StackHead newHead;
newHead.link = newItem;
do {
newItem->next = expected.link;
newHead.count = expected.count + 1;
} while (!head.compare_exchange_weak(expected, newHead));
}
// How to transfer ownership into and out of a unique_ptr
unique_ptr<A> ap(new A); // ap owns the new A
ap.reset(new A); // delete old A and own new A
void f1(A *); // f1 will not del - not interested in managing the lifetime of the object
void f2(A *); // f2 should delete - supposed f2 takes over ownership of the object - it's a transfer of ownership
f1(ap.get()); // pass the pointer ????? doesn't this violate the unique pointer being the only object
// that holds the pointer
f2(ap.release()); // pass ptr and release ownership - will set ap to store nullptr after releasing
// Funcs returning new obj should return a unique_ptr
// because someone needs to own the object after it's created
unique_ptr<A> Afactory();
ap = Afactory();
// you can't copy unique pointers because that would create ambiguous ownership.
// you move them instead
unique_ptr<A> ap2 = move(ap); // basically a promise that you do with what you want with ap
// 'new' still returns a raw pointer to an object - this is bad practice to have an object who's owned
// by not anything that has a destructor that will delete it
auto ap = make_unique<A>(1, 2, 3); // new's the object with constructor argument - returns a unique pointer
// same as
unique_ptr<A> ap(new A(1, 2, 3));
// or use make_shared
// for an array of 10 A elements
auto ap = make_unique<A[]>(10);
// a raw pointer means you are using the object - you are not managing the object
// down the road there will be a observerpointer
// shared_ptr
// only once the last user of the object goes away does the reference count go to zero and object goes away
// a difference from unique_ptr - can copy
// don't create two shared_ptr's from the same object
// one time you are stuck using raw pointers - which is the 'this' pointer
// POINTERS TO FUNCTIONS
// point to the code to that function - so that the compiler can generate a call to that function
int (*fp)(int, int); // *fp can be called with 2 ints
// let's show fp in action
int f(int i, int j) { ... }
fp = &f; // can also say
fp = f; // same thing - assigning 'f' gives the address of the function
auto fp = &f;
fp(2, 3); // language dereference fp for you - no need to dereference with '*'
// the following line sonly works without captures
fp = [](int i, int j) { return i + j; }
// POINTER TO MEMBERS
// more like an offset into A objects
int A::*aip = &A::i; // not an address in the conventional sense
void (A::*afp)(double) = &A::foo;
A *ap = new A;
A a;
// combine the pointer to the ap
ap->*aip = 3; // set ap->i to 3 -- could have been pointing to j but was defined to point to i
(a.*afp)(3.141592); //??? why do you use '.' and not '->'
auto di = make_tuple(2.5, 3, 'c');
cout << get<0>(di) // prints 2.5
cout << get<char>(di); // prints 'c' (C++14)
int three = get<1>(di);
// ??? any reason tuples doesn't support [] indexing??
vector<unique_ptr<Animal>> zoo;
zoo.emplace_back(new Elephant);
zoo.emplace_back(new Zebra);
zoo.emplace_back(new Bear);
cout << "Feading time (f) or Bedtime (b)?";
char c;
cin >> c;
void (Animal::*ap) () = c == 'f' ? &Animal::eat : &Animal::sleep;
for (auto it = zoo.begin(); it != zoo.end(); it++) {
((**it).*ap)(); // can't use '->' with unique ptrs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment