class X
{
public:
int mData;
int *mPtr;
}; class X
{
public:
X(){mData = 0; cout << "X::X()" << endl;}
int mData;
};
class Xs
{
public:
X mX;
int mN;
};
int main()
{
Xs xs;
cout << xs.mX.mData << endl;
cout << xs.mN << endl;
return 0;
} inline Xs::Xs()
{
mX.X::X();
}class Xs
{
public:
Xs()
{
cout << "Xs::Xs()" << endl;
mN = 1;
}
X mX;
int mN;
}; inline Xs::Xs()
{
mX.X::X();
cout << "Xs::Xs()" << endl;
mN = 1;
} class X
{
public:
X(){mData = 0; cout << "X::X()" << endl;}
int mData;
};
class XX : public X
{
public:
int mN;
};
class XXX : public XX
{
};
int main()
{
XX xx;
cout << xx.mData << endl;
cout << xx.mN << endl;
XXX xxx;
return 0;
}class XX : public X
{
public:
XX()
{
mN = 1;
}
XX(int n)
{
mN = n;
}
int mN;
};
int main()
{
XX xx1;
XX xx2(2);
return 0;
} class X
{
public:
virtual ~X()
{
cout << "X::~X()" << endl;
}
virtual void print()
{
cout << "X::print()" << endl;
}
int mData;
};
class XX : public X
{
public:
virtual ~XX()
{
cout << "XX::~XX()" << endl;
}
virtual void print()
{
cout << "XX::print()" << endl;
}
int mN;
};
int main()
{
X *x = new XX;
x->print();
delete x;
return 0;
} int main()
{
X *x = malloc(sizeof(XX));
x->vptr = XX::vtbl;
(x->vptr[1])(x); // 1为print函数在vtbl中的索引
(x->vptr[0](x); // 0为析构函数在vtbl中的索引
free(x);
return 0;
} class X
{
public:
X() {mData = 100;}
int mData;
};
class X1 : virtual public X
{
public:
X1() {mX1 = 101;}
int mX1;
};
class X2 : virtual public X
{
public:
X2() {mX2 = 102;}
int mX2;
};
class XX : public X1, public X2
{
public:
XX() {mXX = 103;}
int mXX;
};
int main()
{
cout << "sizeof(XX): " << sizeof(XX) << endl;
XX xx;
int *p = (int*) &xx;
for (int i = 0; i < sizeof(XX) / sizeof(int); ++i, ++p)
{
cout << *p << endl;
}
return 0;
}原文地址:http://blog.csdn.net/ljianhui/article/details/46247897