// Simple C++ class translated to C with explicit "this" pointers // No virtual functions. No constructor - call set function right // after object (i.e., struct) creation. // CSE 333 lecture demo, 3/16. HP #include #include // class Thing "instance variables" typedef struct thing { int x_; } Thing; // class Thing "member functions" int getX(Thing *this) { return this->x_; } void setX(Thing *this, int x) { this->x_ = x; } int main() { Thing t1; setX(&t1, 17); Thing *t2 = (Thing *)malloc(sizeof(Thing)); setX(t2, 42); int n1 = getX(&t1); int n2 = getX(t2); printf("t1 x = %d, t2 x = %d\n", n1, n2); setX(&t1, 333); printf("t1 x = %d\n", getX(&t1)); return 0; }