Skip to content

Instantly share code, notes, and snippets.

@mysteriouspants
Created December 21, 2011 22:03
Show Gist options
  • Save mysteriouspants/1507924 to your computer and use it in GitHub Desktop.
Save mysteriouspants/1507924 to your computer and use it in GitHub Desktop.
Testing Pointers Stuff
// see http://alexcampbell.github.com//blog/2011/12/19/my-poor-attempt-at-classes/#comment-391786776
#include <iostream>
#include <vector>
#include <cassert>
class C1;
class C2;
class C1 {
public:
std::vector<C2> i0;
};
class C2 {
public:
int32_t i0;
std::vector<C2*> i1;
};
int main(int argc, char *argv[]) {
C1 c10;
C2 c20, c21;
c20.i0 = 1;
c21.i0 = 2;
c10.i0.push_back(c20);
c10.i0.push_back(c21);
// c20 & c21 are copied into the array
assert(c20.i0 == c10.i0[0].i0); // always true
c10.i0[0].i0 = 3;
assert(c20.i0 != c10.i0[0].i0); // they aren't the same any more
c10.i0[0].i1.push_back(&c10.i0[1]);
// stick a link
assert(c10.i0[0].i1[0]->i0 == c10.i0[1].i0); // should be true, right?
c10.i0[1].i0 = 5; // change the value in one spot
assert(c10.i0[0].i1[0]->i0 == c10.i0[1].i0); // should still be true, right?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment