基础知识解惑

ai0fan 2011-02-23 04:30:30
有如下类定义,请问类THouse的InstanceSize是__32___
TPoint = packed record
X: Longint;
Y: Longint;
end;

THouse = class
private
FOwner: string; //8
FPosition: TPoint; //8
FBounds: array[0..2] of Integer; //8
FLessees: TStrings; //4
public
procedure Paint; //4
property Owner: string read FOwner; //0
end;

A、24 B、28 C、32 D、40

寻求相关基础知识。
字符串string为什么是8而procedure方法是4?
...全文
103 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mit1208 2011-02-23
  • 打赏
  • 举报
回复
你可以新建个工程,然后放里边一个Button
在Button的click事件里写入下边的代码来查看数据类型占用的内存
ShowMessage(IntToStr(SizeOf(string)));
wxieyang 2011-02-23
  • 打赏
  • 举报
回复
FOwner是字符串指针
chatop 2011-02-23
  • 打赏
  • 举报
回复
FOwner: string; //4
为什么是4?这个似乎是不定长的吧。
wxieyang 2011-02-23
  • 打赏
  • 举报
回复
你搞错了:
应该是:
THouse = class
private
FOwner: string; //4
FPosition: TPoint; //2 * 4 = 8
FBounds: array[0..2] of Integer; //3 * 4 = 12
FLessees: TStrings; //4
public
procedure Paint; //0
property Owner: string read FOwner; //0
end;

上面标出来的,加起来时28
另外,类中还包含一个隐含指针,也就是self
他指向这个类的类信息
所以 THouse 是 28 + 4 =32

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧