一个疑问,请教高手

imickey 2005-01-12 11:31:08
在动态生成一个组件的时候,我发现一般有三种方法,以TButton为例
1. TButton *tmp = new TButton (this);
2. TButton *tmp = new TButton (NULL);
3. TButton *tmp = new TButton (Application);

请教这三种有什么区别?
...全文
124 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjy1001 2005-01-14
  • 打赏
  • 举报
回复

这么多人重复啊……
从横四海 2005-01-14
  • 打赏
  • 举报
回复
up
过客猫2022 2005-01-14
  • 打赏
  • 举报
回复
很多人讲得都正确!我就不讲了:)
Maconel 2005-01-13
  • 打赏
  • 举报
回复
那是因为TButton的构造函数有3个重载,用NULL做参数的话,就会分不清到底是用的是
TButton(TComponent* AOwner);还是
TButton(void* ParentWindow);
解决方法是写清楚:
TButton* tmp = new TButton((TComponent*)NULL);
或者
TButton* tmp = new TButton((void*)NULL);
imickey 2005-01-13
  • 打赏
  • 举报
回复
但我在用TButton *tmp = new TButton (NULL);的时候,为什么报错呢?
Maconel 2005-01-13
  • 打赏
  • 举报
回复
补充一下,不是父组件不同,是owner不同,
owner和parent是不一样的。
如果你不自己手动释放tmp,那么你指定的owner在被释放时,会替你释放tmp。
parent是指明tmp放在谁的上边,只是显示的问题。
wilsonloveada 2005-01-13
  • 打赏
  • 举报
回复
其实就是他们的父组件不同

TButton *tmp = new TButton (this); 最后在this里释放这个资源,如果是VCL会帮你管理
TButton *tmp = new TButton (Application); VCL帮你管理
TButton *tmp = new TButton (NULL); 你要自己释放

tmp=NULL;
delete tmp;
3996906 2005-01-13
  • 打赏
  • 举报
回复
指定动态创建的管理类,也就是父组件

TButton *tmp = new TButton (this); 最后在this里释放这个资源,如果是VCL会帮你管理
TButton *tmp = new TButton (Application); VCL帮你管理
TButton *tmp = new TButton (NULL); 你要自己释放
csrwgs 2005-01-13
  • 打赏
  • 举报
回复
看各类的构造函数了
我不懂电脑 2005-01-13
  • 打赏
  • 举报
回复
Creates and initializes a TButton instance.

__fastcall virtual TButton(Classes::TComponent* AOwner);

__fastcall TButton(HWND ParentWindow) : Stdctrls::TButtonControl(ParentWindow) { }

Description

Use TButton indirectly, using the new keyword, to programmatically instantiate a button. Buttons added in the form designer are created automatically.

AOwner is the component that is responsible for freeing the button instance. It becomes the value of the Owner property.

ParentWindow is the handle to a non-VCL window that hosts the button control This syntax is used, for example, when exporting the button from a dll.
jemyzhang 2005-01-13
  • 打赏
  • 举报
回复
其实就是他们的父组件不同
Javpp 2005-01-12
  • 打赏
  • 举报
回复
括号里的参数类型可以是如下几种,CB里看到的:
TComponent *AOwner;
void * ParentWindow;
const TButton &(如果生成的是TButton)

不用说了吧,感觉这个参数象个容器.

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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