Comparison of casting between Objective-C and C ++

Good, so this may be a bit of an academic question. Can someone tell me if / how C ++ casting operators can translate to Objective-C ... or how / why they are not needed?

I have not seen C ++ for several years now, and it seems that every time I turn around, they add a few new keywords. I recently introduced C ++ to various casting operators reinterpret_cast, static_cast, dynamic_cast, and const_cast. I'm a little vague when situations arise that you will need to use all of these types of ghosts.

I've been using Objective-C for a year or two and feel pretty comfortable with it. (Before that, he was the main person of C). I am trying to understand why C ++ seems to have all this complexity. Or, in another way, that Objective-C is missing, that it does not (or need?) This many casting types?

+5
source share
4 answers

See this answer to the question When to use static_cast, dynamic_castand reinterpret_cast? by value, each type of cast.

that Objective-C is missing, that it doesn’t (or need?) these many types of castings?

++ , C. , ( - ). ,

  • Objective-C const (const NSObject*), , ++, const_cast .

  • Objective-C , dynamic_cast . ( ObjC -isKindOfClass:.)

  • static_cast reinterpret_cast C, ++. ++ ( ObjC), , no-op:

    #include <cstdio>
    
    struct A {
        int x;
        A() : x(12) {}
    };
    struct B {
        int y;
        B() : y(33) {}
        int get() const { return y; }
    };
    struct C : A, B {
        int z;
        C() : A(), B(), z(41) {}
    };
    
    int main () {
        C* c = new C;
        printf("%d\n", c->get());                       // 33
        printf("%d\n", static_cast<B*>(c)->get());      // 33
        printf("%d\n", reinterpret_cast<B*>(c)->get()); // 12
    }
    
+9

CPlusPlus.com, .

  • reinterpret_cast , . . : , .
  • static_cast , , . , , , , , .
  • dynamic_cast . - , .
  • const_cast - , , . , const ,

, , . , ( !).

C , , .

+1

, , , . , const int* to const char* const. , ++ reinterpret_cast, const const_cast. const void* p; reinterpret_cast<char*>(p), .

dynamic_cast ( ), (.. Base1* Derived, Base1* Base2*, Base2*, - "" ).

static_cast - - (int ↔ float ..), / . , dynamic_cast /, .

, , -, , ( ) , . , . , . , "" C- .

, Obj-C "" . , const_cast, reinterpret_cast static_cast - , -, C. dynamic_cast, ( ), isa.

+1

, C ( Objective C) , ++ - . , , , . , - , , .

, Objective C, dynamic_cast, , , 0 , , isKindOfClass NSObject Objective C, ++ downcasting. , , - Objective C , , downcasting , ++.

+1

All Articles