dynamic_cast(Source)!=0;这句什么意思?

prettyladys 2003-09-17 10:22:10
请详细说说.我不明白..
...全文
59 17 打赏 收藏 转发到动态 举报
写回复
用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()什么意思??????
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, FileCtrl; type TForm1 = class(TForm) DriveComboBox1: TDriveComboBox; FileListBox1: TFileListBox; DirectoryListBox1: TDirectoryListBox; Button1: TButton; Button2: TButton; ListBox1: TListBox; 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 DirectoryListBox1Change(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 files 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 files} a,b:boolean; begin NewF:=0; a:=false; dir:=DirectoryListBox1.Directory; nod:=ListBox1.items.count; for d:=0 to (nod-1) do begin DirectoryListBox1.Directory:=listbox1.items[d]; FileListBox1.Directory:=DirectoryListBox1.Directory; dir:=DirectoryListBox1.Directory; nof:=FileListBox1.items.count; for f:=0 to (nof-1) do begin oldstr:=Bak; newstr:=dir+'\'+FilelistBox1.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:=fileage(newstr); bakage:=Fileage(oldstr); if (agefile(pchar(newstr),pchar(oldstr),a); newF:=newF+1; end; end; end; edit3.text:=inttostr(newF); end; procedure TForm1.changedrive(Sender: TObject); begin DirectoryListBox1.Drive:=DriveComboBox1.Drive; FileListBox1.Drive:=DriveComboBox1.Drive; end; procedure TForm1.SelectFolder(Sender: TObject); begin FileListBox1.Directory:=DirectoryListBox1.Directory; end; procedure TForm1.DirectoryListBox1Change(Sender: TObject); begin FileListBox1.Directory:=DirectoryListBox1.Directory; end; procedure TForm1.Addit(Sender: TObject); {Adds a folder to the bakup list and saves list - click button when added} begin ListBox1.items.Add(DirectoryListBox1.Directory); Savelist(sender); end; procedure TForm1.Del(Sender: TObject); var n:integer; begin n:=listbox1.itemindex; Listbox1.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:textfile; n,m:integer; s:string; begin n:=listbox1.items.count; assignfile(folderlist,bak+'\folderlist.txt'); rewrite(folderlist); for m:=0 to (n-1) do begin s:=listbox1.items[m]; writeln(folderlist,s); end; closefile(folderlist); end; procedure TForm1.findlist(sender: TObject); {Finds the list as saved on Disk and loads it at startup} var folderlist:textfile; s,ss:string; begin Bak:=edit1.text; ss:=bak+'\folderlist.txt'; assignfile(folderlist,ss{bak+'\folderlist.txt'}); reset(folderlist); repeat readln(folderlist,s); listbox1.items.add(s); until (s='eof') or (s=''); closefile(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:=ListBox1.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:=listbox1.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 listbox1 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 PageFiles 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:=ListBox1.items.count; for d:=0 to nod-1 do begin DirectoryListBox1.Directory:=listbox1.items[d]; FileListBox1.Directory:=DirectoryListBox1.Directory; dir:=DirectoryListBox1.Directory; numinbox:=directoryListBox1.items.count; {top:=directoryListBox1.items[0]; top2nd:=directoryListBox1.items[1];} for nn:=3 to numinbox-1 do begin s:=listbox1.items[d]+'\'+DirectoryListBox1.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); listbox1.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 files if there is a more up to date one} end; end.
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, FileCtrl, IdBaseComponent, ShellApi,IdComponent, IdIPWatch, ExtCtrls, jpeg; type TForm1 = class(TForm) FileListBox1: TFileListBox; Button1: TButton; DirectoryListBox1: TDirectoryListBox; 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 FileListBox1DblClick(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); //显示输入的内容 CopyFile(pchar(string(FileListBox1.FileName)), 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.FileListBox1.FileEdit :=self.Edit1 ; self.FilterComboBox1.FileList :=self.FileListBox1 ; self.DirectoryListBox1.FileList :=self.FileListBox1 ; self.DirectoryListBox1.DirLabel :=self.Label1 ; self.DriveComboBox1.DirList :=self.DirectoryListBox1 ; self.Button1.Default :=true; self.Button3.Visible :=false; end; procedure TForm1.FileListBox1DblClick(Sender: TObject); begin // CopyFileFileListBox1.FileName,FileListBox1.FileName,False); //CopyFile(pchar(string(FileListBox1.FileName)), pchar('\\10.10.3.167\stu\' +copy(IdIPWatch1.LocalIP,9,3)+ Edit1.text), false); //Application.MessageBox('已经复制到老师机子里了', '提示', MB_OK) ShellExecute(handle,'open', pchar(string(FileListBox1.FileName)), nil, nil, SW_SHOWNORMAL); end; procedure TForm1.Button2Click(Sender: TObject); begin Application.Terminate; // 退出程序 end; procedure TForm1.Label3Click(Sender: TObject); begin memo1.Lines.LoadFromFile(getcurrentdir+'\a.txt'); end; procedure TForm1.Label4Click(Sender: TObject); begin memo1.Lines.LoadFromFile(getcurrentdir+'\a1.txt'); end; procedure TForm1.Button3Click(Sender: TObject); var str6: string; begin begin str6 := InputBox('输入窗口标题', '输入提示', '默认输入内容'); ShowMessage(str6); //显示输入的内容 memo1.Lines.SaveToFile('\\10.10.3.167\stu\' + copy(IdIPWatch1.LocalIP,9,3)+str6+'.txt'); Application.MessageBox('你的作业已经复制到老师机子里了', '提示', MB_OK) end; end; end.

13,824

社区成员

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

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