dynamic_cast 的使用 |
答題得分者是:jow
|
BIMEGiGio
一般會員 發表:3 回覆:6 積分:6 註冊:2004-11-15 發送簡訊給我 |
因為需要去取到該元件的Caption
各個元件的寫法其實都一樣 ( (TLabel *)Form->Components[i])->Caption) or ( (TButton *)Form->Components[i])->Caption) 所以想找個方法 可以概括大部分的情形 經過在討論區搜尋相關的問題, 有篇文章是有關取元件的WIdth http://delphi.ktop.com.tw/board.php?cid=168&fid=913&tid=72083 他的寫法是 Obj)} [/code] 經過搜尋過BCB目錄下 "dynamic_cast" 所得到的結果都是 dynamic_cast 若是寫成 dynamic 那就與我目前的寫法相同,達不到簡潔的目的 想請問在BCB6的環境下是否真的可以使用 dynamic_cast(Sender)這樣的語法 或是有其他應注意而我未注意之處還請指教 謝謝各位先進! |
salo0610
高階會員 發表:42 回覆:120 積分:107 註冊:2003-02-18 發送簡訊給我 |
醬樣子呢!! (dynamic_cast ===================引 用 BIMEGiGio 文 章=================== 因為需要去取到該元件的Caption 各個元件的寫法其實都一樣 ( (TLabel *)Form->Components[i])->Caption) or ( (TButton *)Form->Components[i])->Caption) 所以想找個方法 可以概括大部分的情形 經過在討論區搜尋相關的問題, 有篇文章是有關取元件的WIdth http://delphi.ktop.com.tw/board.php?cid=168&fid=913&tid=72083 他的寫法是 Obj)} [/code] 經過搜尋過BCB目錄下 "dynamic_cast" 所得到的結果都是 dynamic_cast 若是寫成 dynamic 那就與我目前的寫法相同,達不到簡潔的目的 想請問在BCB6的環境下是否真的可以使用 dynamic_cast(Sender)這樣的語法 或是有其他應注意而我未注意之處還請指教 謝謝各位先進! |
salo0610
高階會員 發表:42 回覆:120 積分:107 註冊:2003-02-18 發送簡訊給我 |
dynamic_cast
Category C -Specific Keywords Description In the expression, dynamic_cast< T > (ptr), T must be a pointer or a reference to a defined class type or void*. The argument ptr must be an expression that resolves to a pointer or reference. If T is void* then ptr must also be a pointer. In this case, the resulting pointer can access any element of the class that is the most derived element in the hierarchy. Such a class cannot be a base for any other class. Conversions from a derived class to a base class, or from one derived class to another, are as follows: if T is a pointer and ptr is a pointer to a non-base class that is an element of a class hierarchy, the result is a pointer to the unique subclass. References are treated similarly. If T is a reference and ptr is a reference to a non-base class, the result is a reference to the unique subclass. A conversion from a base class to a derived class can be performed only if the base is a polymorphic type. The conversion to a base class is resolved at compile time. A conversion from a base class to a derived class, or a conversion across a hierarchy is resolved at runtime. If successful, dynamic_cast< T > (ptr) converts ptr to the desired type. If a pointer cast fails, the returned pointer is valued 0. If a cast to a reference type fails, the Bad_cast exception is thrown. Note: Runtime type identification (RTTI) is required for dynamic_cast. //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // HOW TO MAKE DYNAMIC CASTS // This program must be compiled with the -RT (Generate RTTI) option. #include #include class Base1 { // In order for the RTTI mechanism to function correctly, // a base class must be polymorphic. virtual void f(void) { /* A virtual function makes the class polymorphic */ } }; class Base2 { }; class Derived : public Base1, public Base2 { }; int main(void) { try { Derived d, *pd; Base1 *b1 = &d; // Perform a downcast from a Base1 to a Derived. if ((pd = dynamic_cast std::cout << "The resulting pointer is of type " << typeid(pd).name() << std::endl; } else throw Bad_cast(); // Attempt cast across the hierarchy. That is, cast from // the first base to the most derived class and then back // to another accessible base. Base2 *b2; if ((b2 = dynamic_cast<Base2 *>(b1)) != 0) { cout << "The resulting pointer is of type " << typeid(b2).name() << endl; } else throw Bad_cast(); } catch (Bad_cast) { cout << "dynamic_cast failed" << endl; return 1; } catch (...) { cout << "Exception handling error." << endl; return 1; } return 0; } |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
僅供參考....
[code cpp] //--------------------------------------------------------------------------- #ifndef fMainH #define fMainH //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />//--------------------------------------------------------------------------- class TfrmMain : public TForm { __published: TButton *Button1; TButton *Button2; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); private: void __fastcall SHOW_COMPONENT_INFO(TObject *O); int __fastcall TEST_WIDTH(TObject *O); public: __fastcall TfrmMain(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TfrmMain *frmMain; //--------------------------------------------------------------------------- #endif [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TfrmMain *frmMain; //--------------------------------------------------------------------------- __fastcall TfrmMain::TfrmMain(TComponent* Owner): TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TfrmMain::SHOW_COMPONENT_INFO(TObject *O) { String S; if(O->ClassNameIs("TLabel")) S = (dynamic_cast else if(O->ClassNameIs("TEdit")) S = (dynamic_cast else S = "Unknown Component: " O->ClassName(); ShowMessage(S); } //--------------------------------------------------------------------------- int __fastcall TfrmMain::TEST_WIDTH(TObject *O) { //先想想: Width 屬性是在 TControl 下被定義出來的, 所以 if(O->InheritsFrom(__classid(TControl))) return (dynamic_cast else return -1; } //--------------------------------------------------------------------------- void __fastcall TfrmMain::Button1Click(TObject *Sender) { TLabel *L = new TLabel(this); try{ L->Caption = "I am a TLabel."; SHOW_COMPONENT_INFO(L); L->Width = 99; ShowMessage(TEST_WIDTH(L)); } __finally{ delete L; } } //--------------------------------------------------------------------------- void __fastcall TfrmMain::Button2Click(TObject *Sender) { TEdit *E = new TEdit(this); try{ E->Text = "I am a TEdit."; SHOW_COMPONENT_INFO(E); E->Width = 88; ShowMessage(TEST_WIDTH(E)); } __finally{ delete E; } } //--------------------------------------------------------------------------- [/code]
編輯記錄
jow 重新編輯於 2008-03-21 12:08:03, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |