using namespace std;
struct Base
{
Base(
int
a,
long
b) : m_a(a), m_b(b) {}
int
m_a;
virtual void
print
() { cout <<
"m_a = "
<< m_a <<
", m_b = "
<< m_b << endl; }
long
m_b;
virtual ~Base() { cout <<
"~Base() called"
<< endl; }
};
struct Derived : public Base
{
Derived(
int
a,
long
b,
int
c) : Base(a, b), m_c(c) {}
virtual ~Derived() { cout <<
"~Derived() called"
<< endl; }
void
print
() { cout <<
"m_a = "
<< m_a <<
", m_b = "
<< m_b <<
", m_c = "
<< m_c << endl; }
int
m_c;
};
int
main()
{
Derived d(
1
,
2
,
3
);
Base
*
pb
=
&d;
cout << sizeof(d) << endl;
cout <<
*
(
int
*
)((
long
*
)pb
+
3
) << endl;
void (Base::
*
func)()
=
&Base::
print
;
(pb
-
>
*
func)();
((void (
*
)())
*
(
long
*
)
*
(
long
*
)&d)();
pb
=
new Derived(
4
,
5
,
6
);
cout << (
*
(
long
*
)&d
=
=
*
(
long
*
)pb) << endl;
delete pb;
cout <<
"---- 楚河汉界 ----"
<< endl;
}