1 2 3 4 5 6 7 8 9 10 11 | class A { public: void foo(float value) { /*something cool with the value */ } }; int main() { A theA; A::foo(theA, 10.f); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <stdio.h> struct Apple { float weight; void print() { printf("weight = %f\n", weight); } }; struct AppleIterator { AppleIterator(Apple* apple) : apple(apple) {} AppleIterator& operator ++ () { apple++; return *this; } Apple* operator -> () { return apple; } bool operator == (const AppleIterator& other) { return apple == other.apple; } bool operator != (const AppleIterator& other) { return !(*this == other); } private: Apple* apple; }; struct Basket { Basket() { for (int i=0; i<10; i++) apples[i].weight = i; } AppleIterator begin() { return AppleIterator(apples); } AppleIterator end() { return AppleIterator(apples + 10); } private: Apple apples[10]; }; int main() { Basket b; for (auto it = b.begin(); it != b.end(); ++it) { it->print(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | struct A { int data1; int data1; void (*foo)(A* this_, float value); }; void A_foo(A* this_, float value) { // stuff } void A_init(A* this_, int data1, int data2) { this_->data1 = data1; this_->data1 = data1; this_->foo = A_foo; } int main() { A obj; A_init(&obj); obj->foo(&A); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Object { public: Object() {} int foo(float value) { return (int)value; } private: int data; }; int main() { Object o; int i = o.foo(0); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | struct Object { int data; }; void Object_ctor(Object* this) { this->data = 0; } int Object_foo(Object* this, float value) { return (int)value; } int main() { Object o; Object_ctor(&o); int i = Object_foo(&o, 0); return 0; // no Object_dtor(&o) in this case (or maybe one that sets value to zero, I can't remember) } |