עוד פעם על castים, בבקשה

cyberia2ooo

New member
השאלה היא מה הטיפוס של

m_objectPtr אם קיים קשר כלשהו כין הטיפוס של m_objectPtr לבין Interface* אם כן קיים קשר, אחת מהצורות שאמרת אמורה לעבוד לך static_cast הייתה עובדת אם יש קשר כלשהו בין הערכים שאתה מנסה להעביר. אם העברה היא בין אובייקטים פולימורפיזם (אובייקטים שיש ביניהם קשר של ירושה, ופונקציות וירטואליות) אז dynamic_cast היה צריך לעבוד אם אין שום קשר וואט סו אבר בין הערכים... אז יש לך בשביל את זה את reinterpret_cast
 

debillyg

New member
casing in C

When you are using the c++ casing you are acctualy calling a template function, hence the call <return type> <casting type -function name> <return type as parameter to the template function (function parameter as the type to cast from) dynamic_cast trys to cast from one type to anther at run time but and this is a big but, since this is a template function the complier "knows" at compile time (since template are instanciate at complie type) if this casing is resnable. static_cast limit itself to casing at complie time only and it dosnt care about run time type (RTTI), so if it can buit the return type object out of the cast from object it will cast from. Reinerprt_cast is more like the C cast, you are telling the compiler that you know that in C++ you cannot instanciate return type from the cast from type but you as the program "knows" better. There is also the const_cast that tell the complier to be like the static_cast and also to remove the const from the cast from type. The C cast is simply to say to the complier to do what you like to do without any checking so it is better not to use it. By the dynamic_cast not returning NULL simply because in C++ NULL is not defined. Invalid pointer in C++ is 0, using NULL is not part of the standard, the only reasun you don't get an error, is because all C++ compiler support C as well. To Sum it up - static_cast when you have 2 object without relation such as int and char that the complier knows how to create one from the other. dynamic_cast when you want to cast based on base and derived relation based on run time type const_cast when you like to remove const restriction
 
למעלה