社区
C++ Builder
帖子详情
dynamic_cast
(Source)!=0;这句什么意思?
prettyladys
2003-09-17 10:22:10
请详细说说.我不明白..
...全文
59
17
打赏
收藏
dynamic_cast<TFileListBox*>(Source)!=0;这句什么意思?
请详细说说.我不明白..
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
17 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
jiangchun_xn
2003-09-18
打赏
举报
回复
2分?呵呵。。。
IAMCDYY2003
2003-09-18
打赏
举报
回复
说简单点就是一种安全转换
winkiky
2003-09-18
打赏
举报
回复
类型转换:dynamic_cast<T>(x)补充:
如果x是指针,若转换失败返回一个NULL指针,如果x是引用,若转换失败就会抛出一个bad_cast的异常
winkiky
2003-09-18
打赏
举报
回复
jiangchun_xn(如果能再遇到你)说的很详细了,作一点补充就是对于指针的判断最好不用0,而该用NULL!
jiangchun_xn
2003-09-18
打赏
举报
回复
类型转换符dynamic_cast,它被用于安全地沿着类的继承关系向下进行类型转换。这就是说,你能用dynamic_cast把指向基类的指针或引用转换成指向其派生类或其兄弟类的指针或引用,而且你能知道转换是否成功。失败的转换将返回空指针(当对指针进行类型转换时)或者抛出异常(当对引用进行类型转换时)
dynamic_casts在帮助你浏览继承层次上是有限制的。它不能被用于缺乏虚函数的类型上,也不能用它来转换掉constness:
如你想在没有继承关系的类型中进行转换,如果是为了去除const,用const_cast
----节选C++条款Item M2:尽量使用C++风格的类型转换
bobbycn
2003-09-18
打赏
举报
回复
正如 ThinkX 所说:"不是什么宏,而是 C++中内置的四种转换之一,用于向下转换。"
dynamic_cast<TFileListBox*>(Source)!=0;
判断Source能不能转换成TFileListBox指针,不能返回空。
如Source中指向TFileListBox基类的指针,而且基类定有虚函数时,
dynamic_cast<TFileListBox*>(Source)返回TFileListBox指针;否则返回空。
FILENAME.C_STR()
FILENAME是什么类别?
windindance
2003-09-18
打赏
举报
回复
如果Source是一个TFileListBox,就相当于TFileListBox(Source)强制转换
否则返回NULL。
所有时候应该使用dynamic_cast代替传统的强制转换。
传统的强制转换无论Source是什么,都转换为TFileListBox。
假如你把一个其他的东西转换为TFileListBox,就很容易产生内存错误。
jiangchun_xn
2003-09-18
打赏
举报
回复
如果转化失败,就返回NULL。
Source是TFileListBox的基类指针。
fantasylu
2003-09-18
打赏
举报
回复
能详细点吗?
ThinkX
2003-09-18
打赏
举报
回复
dynamic_cast<TFileListBox*>(Source);
不是什么宏,而是 C++中内置的四种转换之一,用于向下转换。
jarodmike
2003-09-18
打赏
举报
回复
dynamic_cast<TFileListBox*>(Source)!=0
这句话的意思是对动态转换source源中的指针TFileListBox*>进行判断.
prettyladys
2003-09-18
打赏
举报
回复
UP
aliker
2003-09-18
打赏
举报
回复
dynamic_cast<TFileListBox*>(Source)!=NULL(最好不要用0)
dynamic_cast<T *>P
测试P是否为T类型的指针,如果是,返回T类型的指针,否则返回NULL
void __fastcall TForm1::SomeButtonClick(TObject *Sender)
{
if(dynamic_cast<TButton*>(Sender))
ShowMessage("这个事件是Button触发的!");
if(dynamic_cast<TSpeedButton*>(Sender))
ShowMessage("这个事件是SpeedButton触发的!");
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
SomeButtonClick(Sender);
}
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
SomeButtonClick(Sender);
}
如果你有一个按钮和快速按钮,他们触发的事件都一样,如果分别为按钮和快速按钮各写一个事件处理函数,代码重复,显然没有必要。这时可以为它们指定同一个事件处理函数(如上面的SomeButtonClick。如果你要区分是哪个控件触发的,则可以用dynamic_cast<T *>P测试一下。
FILENAME.c_str()
.c_str()返回AnsiString类的字符指针
如AnsiString FILENAME="c:\\1.txt";
char *p;
p=FILENAME.c_str()返回C类型的字符指针。比如有些WinAPI函数的参数为char *,直接用AnsiString不行,得转化一下。
AnsiString FileName="c:\\1.txt";
FILE *fp;
fp=fopen(FileName.c_str(),"r+");
guanshangming
2003-09-17
打赏
举报
回复
dynamic_cast<TFileListBox*>(Source)
判断安全类型转换是否成功,成功返加非0
FILENAME.C_STR()
我只知道AnsiString中有个成员函数 c_str()意思是取字符串的地址
tongjin9
2003-09-17
打赏
举报
回复
source是一個變量吧
dynamic_cast<TFileListBox*>(Source)應該是類型轉換吧,dynami_cast應該是一個宏,規定
轉換的樣式
FILENAME.C_STR(),好像是獲得filename得地址的意思吧
prettyladys
2003-09-17
打赏
举报
回复
人都为分吗?我是不会..........哎.
prettyladys
2003-09-17
打赏
举报
回复
还有FILENAME.C_STR()什么意思??????
最简单的MP3播放器源码
适合Delphi初学者。所用控件包括:TDriveComboBoxTDirectory
ListBox
TFilterComboBoxT
File
ListBox
TMediaPlayerTeditTTrackBar
delphi7系统文件备份系统
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
File
Ctrl; type TForm1 = class(TForm) DriveComboBox1: TDriveComboBox;
File
ListBox
1: T
File
ListBox
; Directory
ListBox
1: TDirectory
ListBox
; Button1: TButton; Button2: TButton;
ListBox
1: T
ListBox
; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Edit1: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; Label9: TLabel; Edit2: TEdit; Label10: TLabel; Label11: TLabel; Edit3: TEdit; Label12: TLabel; procedure Start(Sender: TObject); procedure changedrive(Sender: TObject); procedure SelectFolder(Sender: TObject); procedure Directory
ListBox
1Change(Sender: TObject); procedure Addit(Sender: TObject); procedure Del(Sender: TObject); procedure Savelist(Sender: TObject); procedure foldercheck(Sender: TObject); procedure Search(Sender: TObject); procedure findlist(sender: TObject); procedure main(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Bak:string; implementation {$R *.DFM} procedure TForm1.Start(Sender: TObject); {copies
file
s when backup required only replaces ones with newer time date} var dir,newstr,oldstr,s,ss:string; d,f,n,nod,nof,age,bakage,len,lendir,newF:integer; {number of directories ,number of
file
s} a,b:boolean; begin NewF:=0; a:=false; dir:=Directory
ListBox
1.Directory; nod:=
ListBox
1.items.count; for d:=0 to (nod-1) do begin Directory
ListBox
1.Directory:=
listbox
1.items[d];
File
ListBox
1.Directory:=Directory
ListBox
1.Directory; dir:=Directory
ListBox
1.Directory; nof:=
File
ListBox
1.items.count; for f:=0 to (nof-1) do begin oldstr:=Bak; newstr:=dir+'\'+
File
listBox
1.items[f]; lendir:=length(dir); {oldstr:=Bak+'\'+copy(dir,4,len-3);} n:=pos('\',dir); s:=copy(dir,n+1,lendir); n:=pos('\',s); repeat ss:=copy(s,1,n-1); oldstr:=oldstr+'\'+ss; b:=DirectoryExists(oldstr); if b=false then mkdir(oldstr); len:=length(oldstr); s:=copy(dir,len-2,lendir); n:=pos('\',s); until n=0; len:=length(newstr); oldstr:=bak+'\'+copy(newstr,4,len-3); age:=
file
age(newstr); bakage:=
File
age(oldstr); if (age
file(pchar(newstr),pchar(oldstr),a); newF:=newF+1; end; end; end; edit3.text:=inttostr(newF); end; procedure TForm1.changedrive(Sender: TObject); begin Directory
ListBox
1.Drive:=DriveComboBox1.Drive;
File
ListBox
1.Drive:=DriveComboBox1.Drive; end; procedure TForm1.SelectFolder(Sender: TObject); begin
File
ListBox
1.Directory:=Directory
ListBox
1.Directory; end; procedure TForm1.Directory
ListBox
1Change(Sender: TObject); begin
File
ListBox
1.Directory:=Directory
ListBox
1.Directory; end; procedure TForm1.Addit(Sender: TObject); {Adds a folder to the bakup list and saves list - click button when added} begin
ListBox
1.items.Add(Directory
ListBox
1.Directory); Savelist(sender); end; procedure TForm1.Del(Sender: TObject); var n:integer; begin n:=
listbox
1.itemindex;
Listbox
1.items.Delete(n); end; procedure TForm1.Savelist(Sender: TObject); {saves as bakup disk\bakup folder\folderlist.txt to disk the list of folders requiring bakup on } var folderlist:text
file
; n,m:integer; s:string; begin n:=
listbox
1.items.count; assign
file
(folderlist,bak+'\folderlist.txt'); rewrite(folderlist); for m:=0 to (n-1) do begin s:=
listbox
1.items[m]; writeln(folderlist,s); end; close
file
(folderlist); end; procedure TForm1.findlist(sender: TObject); {Finds the list as saved on Disk and loads it at startup} var folderlist:text
file
; s,ss:string; begin Bak:=edit1.text; ss:=bak+'\folderlist.txt'; assign
file
(folderlist,ss{bak+'\folderlist.txt'}); reset(folderlist); repeat readln(folderlist,s);
listbox
1.items.add(s); until (s='eof') or (s=''); close
file
(folderlist); end; procedure TForm1.foldercheck(Sender: TObject); {check folders in
Listbox
and makes ones not already existing} var n,nod,d:integer; s,sFront,sTemp:string; b:boolean; begin nod:=
ListBox
1.items.count; {first see if all folders exist in Bakup folder starting from the top make any not existing - ie sets up the Bakup folder structure} for d:=0 to nod-1 do begin s:=
listbox
1.items[d]; s:=s+'\'; delete(s,1,3); sFront:=Bak; repeat n:=pos('\',s); if n<>0 then begin sTemp:=copy(s,1,n-1); sFront:=sFront+'\'+sTemp; b:=DirectoryExists(sFront); if b=false then MkDir(sFront); Delete(s,1,n); end; until n=0; end; end; procedure TForm1.Search(Sender: TObject); {Takes folder list in
listbox
1 and looks to see if one next level down has new folders. If yes then it makes the new folder and adds it to the
listbox
this enables bakup of new web pages with a Page
File
s folder then saves list Does not seem to add to list box new directories found so that it digs deeper next time appears to add directory tried this saw it in .txt
file
(notpad) but on closing it must save in it original form as it has them gone has this to do with the blank lie that gets added in??????????????????} var nod,numinbox,nn,d,NewDir,len:integer; s,ss,dir:string; b:boolean; begin NewDir:=0; nod:=
ListBox
1.items.count; for d:=0 to nod-1 do begin Directory
ListBox
1.Directory:=
listbox
1.items[d];
File
ListBox
1.Directory:=Directory
ListBox
1.Directory; dir:=Directory
ListBox
1.Directory; numinbox:=directory
ListBox
1.items.count; {top:=directory
ListBox
1.items[0]; top2nd:=directory
ListBox
1.items[1];} for nn:=3 to numinbox-1 do begin s:=
listbox
1.items[d]+'\'+Directory
ListBox
1.items[nn]; b:=DirectoryExists(s); if b=true then begin len:=length(s); ss:=copy(s,4,len); ss:=Bak+'\'+ss; b:=DirectoryExists(ss); if b=false then begin mkdir(ss);
listbox
1.items.add(s); NewDir:=NewDir+1; end; end; end; end; Edit2.text:=inttostr(NewDir); savelist(sender); end; procedure TForm1.main(Sender: TObject); begin findlist(sender); {find list of directories from disk}; foldercheck(sender);{check directories from list exist and if not make them} search(sender); {search for folder one level down} start(sender); {save
file
s if there is a more up to date one} end; end.
delphi 基本控件T
ListBox
的应用
delphi 基本控件T
ListBox
的应用 delphi7编译测试通过 delphi 式微
学生上课传输文件系统程序
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
File
Ctrl, IdBaseComponent, ShellApi,IdComponent, IdIPWatch, ExtCtrls, jpeg; type TForm1 = class(TForm)
File
ListBox
1: T
File
ListBox
; Button1: TButton; Directory
ListBox
1: TDirectory
ListBox
; DriveComboBox1: TDriveComboBox; FilterComboBox1: TFilterComboBox; Edit1: TEdit; Label1: TLabel; Button2: TButton; Label2: TLabel; Memo1: TMemo; Label3: TLabel; IdIPWatch1: TIdIPWatch; Label4: TLabel; Button3: TButton; Edit2: TEdit; Label5: TLabel; Label6: TLabel; Label7: TLabel; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure
File
ListBox
1DblClick(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Label3Click(Sender: TObject); procedure Label4Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; a1:integer; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var str5: string; begin begin str5 := InputBox('输入姓名', 'Name:', ''); // Application.Terminate; // 退出程序 if str5 ='' then showmessage('没有输入姓名,请重新输入') else begin ShowMessage(str5); //显示输入的内容 Copy
File
(pchar(string(
File
ListBox
1.
File
Name)), pchar('\\10.10.3.167\stu\' + str5+copy(IdIPWatch1.LocalIP,9,3)+Edit1.text), false); Application.MessageBox('已经复制到老师机子里了', '提示', MB_OK) // Application.Terminate; // 退出程序 end ; end; end; procedure TForm1.FormCreate(Sender: TObject); begin self.
File
ListBox
1.
File
Edit :=self.Edit1 ; self.FilterComboBox1.
File
List :=self.
File
ListBox
1 ; self.Directory
ListBox
1.
File
List :=self.
File
ListBox
1 ; self.Directory
ListBox
1.DirLabel :=self.Label1 ; self.DriveComboBox1.DirList :=self.Directory
ListBox
1 ; self.Button1.Default :=true; self.Button3.Visible :=false; end; procedure TForm1.
File
ListBox
1DblClick(Sender: TObject); begin // Copy
File
(
File
ListBox
1.
File
Name,
File
ListBox
1.
File
Name,False); //Copy
File
(pchar(string(
File
ListBox
1.
File
Name)), pchar('\\10.10.3.167\stu\' +copy(IdIPWatch1.LocalIP,9,3)+ Edit1.text), false); //Application.MessageBox('已经复制到老师机子里了', '提示', MB_OK) ShellExecute(handle,'open', pchar(string(
File
ListBox
1.
File
Name)), nil, nil, SW_SHOWNORMAL); end; procedure TForm1.Button2Click(Sender: TObject); begin Application.Terminate; // 退出程序 end; procedure TForm1.Label3Click(Sender: TObject); begin memo1.Lines.LoadFrom
File
(getcurrentdir+'\a.txt'); end; procedure TForm1.Label4Click(Sender: TObject); begin memo1.Lines.LoadFrom
File
(getcurrentdir+'\a1.txt'); end; procedure TForm1.Button3Click(Sender: TObject); var str6: string; begin begin str6 := InputBox('输入窗口标题', '输入提示', '默认输入内容'); ShowMessage(str6); //显示输入的内容 memo1.Lines.SaveTo
File
('\\10.10.3.167\stu\' + copy(IdIPWatch1.LocalIP,9,3)+str6+'.txt'); Application.MessageBox('你的作业已经复制到老师机子里了', '提示', MB_OK) end; end; end.
最简单的图像浏览器源码
适合Delphi初学者。所用控件包括: TDriveComboBox TDirectory
ListBox
TFilterComboBox T
File
ListBox
TImage
C++ Builder
13,824
社区成员
102,679
社区内容
发帖
与我相关
我的任务
C++ Builder
C++ Builder相关内容讨论区
复制链接
扫一扫
分享
社区描述
C++ Builder相关内容讨论区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章