int main(int argc, char** argv) { Person ed = person_new("Ed MacDonald"); Person john = person_new("John Doe"); printf("Hello %s.\n", ed->vtable->getName(ed)); ed->vtable->setName(ed, john->vtable->getName(john)); printf("Hello %s.\n", ed->vtable->getName(ed)); //Using the dispatch macro dispatch(ed, setName, "Jane Doe"); printf("Hello %s.\n", dispatch(ed, getName)); person_delete(ed); person_delete(john); }
int main(int argc, char **argv) { person *base; pilot *sub; eina_init(); eina_magic_string_set(BASETYPE_MAGIC, "person"); eina_magic_string_static_set(SUBTYPE_MAGIC, "pilot"); base = person_new("Tyrol"); sub = pilot_new("thrace", "starbuck"); print_person(base); print_person((person *)sub); print_pilot(base); //BAD print_pilot(sub); eina_shutdown(); }
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HINSTANCE hInstance = GetModuleHandle(NULL); static HWND hLabel, hStaticKey, hStaticValue, hWndList; static person_t person1 = NULL, person2 = NULL, person3 = NULL, person4 = NULL; switch (msg) { case WM_CREATE: { hLabel = CreateWindowEx( 0, (TCHAR*)"STATIC", (TCHAR*)"List of persons", WS_CHILD | WS_VISIBLE, 10, 10, 100, 26, hwnd, (HMENU)ID_LABEL, hInstance, NULL ); hStaticKey = CreateWindowEx(0, WC_STATIC, "Name", WS_CHILD | WS_VISIBLE, 220, 40, 170, 23, hwnd, (HMENU)HSTATICKEY, hInstance, NULL); hStaticValue = CreateWindowEx(0, WC_STATIC, "Value", WS_CHILD | WS_VISIBLE, 220, 70, 170, 23, hwnd, (HMENU)HSTATICVALUE, hInstance, NULL); hWndList = CreateWindowW( L"listbox", L"Update timer", WS_VISIBLE | WS_CHILD | WS_VSCROLL | ES_AUTOVSCROLL | WS_BORDER | LBS_NOTIFY, 10, 40, 185, 125, hwnd, (HMENU)ID_LB, NULL, NULL ); person1 = person_new("Ivan", "Ivanov", 1995); person_insertToList(person1, hWndList); person2 = person_new("Petr", "Petrov", 1944); person_insertToList(person2, hWndList); person3 = person_new("Dmitriy", "Dmitrenko", 1986); person_insertToList(person3, hWndList); person4 = person_new("Maxim", "Maximov", 1954); person_insertToList(person4, hWndList); } break; case WM_COMMAND: { switch (LOWORD(wParam)) { case ID_LB: switch (HIWORD(wParam)) { case LBN_SELCHANGE: { HWND hLabel = GetDlgItem(hwnd, ID_LABEL); HWND control = GetDlgItem(hwnd, ID_LB); char text[256]; int count = SendMessage(control, LB_GETCOUNT, 0, 0); int iSelected = -1; for (int i = 0; i < count; i++) { if (SendMessage(control, LB_GETSEL, i, 0) > 0) { iSelected = i; char key[100] = ""; SendMessage(control, LB_GETTEXT, (WPARAM)iSelected, (LPARAM)key); SetWindowText(hStaticKey, key); char * value = (char*)SendMessage(control, LB_GETITEMDATA, (WPARAM)iSelected, 0); SetWindowText(hStaticValue, value); } } } break; } break; } } break; case WM_CLOSE: DestroyWindow(hwnd); DestroyWindow(hStaticKey); DestroyWindow(hStaticValue); DestroyWindow(hWndList); DestroyWindow(hLabel); person_free(person1); person_free(person2); person_free(person3); person_free(person4); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }