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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <stdio.h> class Interface { virtual void f() = 0; }; class SuperA { int a; }; class SuperB { int b; }; class SuperC { int c; virtual void f() {}; }; class SuperD { int d; virtual void fd() {}; }; class A_B : public SuperA, public SuperB { int ab; }; class A_Interface : public SuperA, public Interface { int ai; void f() {}; }; class A_C : public SuperA, public SuperC { int ac; }; class C_D : public SuperC, public SuperD { int cd; }; int main() { A_B *ab_pointer = new A_B{}; A_Interface *ai_pointer = new A_Interface{}; A_C* ac_pointer = new A_C{}; C_D* cd_pointer = new C_D{}; printf("ab: %x a: %x b: %x\n", ab_pointer, (SuperA*)ab_pointer, (SuperB*)ab_pointer); // ab: 835eb8 a: 835eb8 b: 835ebc 왼쪽 부모 주소랑 자식 주소가 같음 printf("ai: %x a: %x i: %x\n", ai_pointer, (SuperA*)ai_pointer, (Interface*)ai_pointer); // ai: 835ef0 a: 835ef4 i: 835ef0 오른쪽 부모 주소랑 자식 주소가 같음. 순수 추상 클래스 상속이 우선되나? 왜? printf("ac: %x a: %x c: %x\n", ac_pointer, (SuperA*)ac_pointer, (SuperC*)ac_pointer); // ac: 8359f8 a: 835a00 c: 8359f8 오른쪽 부모 주소랑 자식 주소가 같음. 가상 함수 테이블을 자식과 공유하기 위함이었던 것 같다. printf("cd: %x c: %x d: %x\n", cd_pointer, (SuperC*)cd_pointer, (SuperD*)cd_pointer); // cd: 1115a38 c: 1115a38 d: 1115a40 왼쪽 부모 주소랑 자식 주소가 같음. 예상대로 부모 둘 다 가상 함수 테이블을 갖고 있으면 왼쪽이 우선된다. return 0; } | cs |
눈 아프네 ㅇㅅㅇ... 예쁘게 가져오는 법 없나