예전에 했던 포인터로 캡슐화를 깨보자! 에서 파생된 건데 생각한 것 만큼 잘 안되네요 -_-;;

프갤흰님들의 도움이 필요한 부분입니다.




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
#include <iostream>
 
using namespace std;
 
class Widget
{
public:
    Widget(int val, char ch, unsigned short num,  char* str) :_val(val),_ch(ch),_num(num)
    {
        int length = strlen(str) + 1;
        _str = new char[length];
        strcpy(_str, str);
    }
 
    ~Widget()
    {
        delete[] _str;
    }
 
    virtual void VirtualFunc()
    {
        cout << "I'm the first virtual function " << endl;
    }
private:
    int _val;
    char _ch;
    unsigned short _num;
    char* _str;
};
 
int main(void)
{
    Widget w(128,'a',64"ABCD");
    char* pw = reinterpret_cast<char*>(&w);
    
    printf("%d "*(int*)(pw + 4));
    printf("%c "*(char*)(pw + 8));
    printf("%d "*(unsigned short*)(pw + 9));
    printf("%s "*(char**)(pw + 11));
 
    return 0;
}
cs


위에는 제가 생각한 Widget w의 메모리 구조인데 -_-; 잘 안되네요.


*(unsigned short*)(pw+10)

*(char**)(pw+12); 

로 바꾸니까 잘 되긴 하는데


왜 왜 +9가 아니라 +10인지 몰겠심덩..