שאלה לגבי casting ב- ++C

voguemaster

New member
שאלה לגבי casting ב- ++C

אני מעוניין לדעת יותר על ההבדלים בין סוגי ה-casting השונים שיש ב ++C ולמה הם טובים. כמו כן, יש אינדיבידואלים מסוימים שטוענים שחלקם הם פשוט no no אחד גדול, לדוגמא dynamic_cast. מה דעתכם ?
 

annefan

New member
כאב ראש

Sorry for the english: 1. static_cast - the simplest. Used to cast from int to double and such. No casting between Class to int and such. 2. const_cast - still simple. Used to cast away either the constness of a variable or the volatile-ness of it. 3. reinterpret_cast - dangerous. Casts from each type to another with no guarantee that it'll work!! 4. dynamic_cast - headache. Casts a super-class pointer or reference to a sub-class or another sub-class (brother). The classes should be virtual! On VC6, at least, I saw that he sometimes requires to enable RTTI in the project settings.
 

annefan

New member
מי גאון של אמא??? (הודעה מיושרת)

Sorry for the english: 1. static_cast - the simplest. Used to cast from int to double and such. No casting between Class to int and such. 2. const_cast - still simple. Used to cast away either the constness of a variable or the volatile-ness of it. 3. reinterpret_cast - dangerous. Casts from each type to another with no guarantee that it'll work!! 4. dynamic_cast - headache. Casts a super-class pointer or reference to a sub-class or another sub-class (brother). The classes should be virtual! On VC6, at least, I saw that he sometimes requires to enable RTTI in the project settings.​
 

DadleFish

New member
קצת על dynamic_cast

כשמו כן הוא - הוא דינמי, ולכן עושה בדיקות ב-runtime, ולכן באמת צריך RTTI, בלי קשר לקומפיילר. הוא תמיד יצטרך RTTI. היתרון הגדול של dynamic_cast זה שהוא עושה בדיקות, ולכן הקוד הבא:
class A {}; class B: public A {}; void f() { A *a = new B; B *b = dynamic_cast<B *>a; }​
יהיה יותר בטוח מהמקבילה שלו ב-C (כלומר סתם casting פשוט) או משימוש ב-static_cast ודומיו - פשוט כי אם a לא יכול להיות B, יוחזר לך NULL.
 

alexrait1

New member
casting ב c++ = כתיבה גרועה

נהוג להשתמש ב casting בעיקר ב c, ב c++ לעומת זאת מקובל להשתמש בtemplates,overloading וכו' על מנת להמנע מcasting.
 

voguemaster

New member
ספר את זה ל-Qt

ואני אתן דוגמא: אם אתה בונה עץ ב-Qt, שזה לא דבר יוצא מגדר הרגיל, סה"כ QListView מסכן, אתה צריך להכניס לתוכו QListViewItemים. כל אחד מהחבר'ה האלה מייצג NODE בעץ שלך. אם אתה רוצה לשמור עוד מידע בכל NODE אתה צריך לרשת מ-QListViewItem שלהם. למעשה זו גם ההצעה שלהם (יש להם דוגמא של file browser שעושה בדיוק את זה). תאר לך שאתה מקליק על NODE מסוים ו-Qt מחזירה לך מצביע אל ה-QListViewItem. בוא עוד נגיד שאתה רוצה להציג אחרי הקליק מידע מסוים ששייך לאותו ITEM אבל נמצא כמובן במחלקת הבן (כמו שם קובץ, גודל וכד'). יש לך את המצביע לאובייקט האב, מה תעשה ? חשבתי על זה ואני לא מצאתי פיתרון. אולי אני לא תותח-על ב ++C וזו הסיבה.
 

hatulflezet

New member
אתה מתכוון אולי למשהו כזה?

void DDirView::slotFolderSelected( QListViewItem *i ) { if ( !i || !showDirsOnly() ) return; DDirectory *dir = (DDirectory*)i; emit folderSelected( dir->fullName() ); }​
מה שמפריע לך זה ה-CASTING ל-DDIRECTORY?
 

DNile

New member
איך אני שונא שאנשים משננים כל מיני

דברים שהם קראו בספרים או שהמרצים שלהם אמרו להם...
 

ברנדל

New member
תגיד

הסיפור עם בל זה נכון? מי המציא את הטלפון? לא שמעתי על זה
 
למעלה