65,208
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
class Widget
{
public:
Widget()
{
std::cout << "Widget" << std::endl;
}
~Widget()
{
std::cout << "~Widget" << std::endl;
}
};
int main()
{
static const int num = 3;
Widget *p[num];
for (int i=0; i<num; i++) {
p[i] = new Widget();
}
for (int i=0; i<num; i++) {
delete p[i];
}
system("pause");
return 0;
}
#include <iostream>
struct Widget
{
Widget()
{
std::cout << "ctor:" << this << std::endl;
}
~Widget()
{
std::cout << "dtor:" << this << std::endl;
}
};
int main()
{
std::cout <<"size of Widget:" << sizeof(Widget) << std::endl;
Widget w[5];
std::cout << "start of w:" << w << std::endl;
return 0;
}
运行结果:
C:\demo>cl /nologo /W4 /EHsc demo.cpp
demo.cpp
C:\demo>demo
size of Widget:1
ctor:001FFCB4
ctor:001FFCB5
ctor:001FFCB6
ctor:001FFCB7
ctor:001FFCB8
start of w:001FFCB4
dtor:001FFCB8
dtor:001FFCB7
dtor:001FFCB6
dtor:001FFCB5
dtor:001FFCB4
C:\demo>
自己写代码验证一下。 以上运行结果仅表明我的编译器是这样的。