Objective-C和C ++之间的转换比较 [英] casting comparison between Objective-C and C++

查看:134
本文介绍了Objective-C和C ++之间的转换比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这可能是一个学术问题。有人可以告诉我是否/如何C ++的转换操作符可能翻译为Objective-C ...或如何/为什么它们不是必要的?

Okay, so this might be a bit of an academic question. Can someone tell me if/how C++'s casting operators might translate to Objective-C... or how/why they're not necessary?

我已经出与C ++的循环几年了,似乎每次我转身他们添加了一些新的关键字。我最近介绍了C ++的各种转换操作符reinterpret_cast,static_cast,dynamic_cast和const_cast。我真的有点模糊,当这些情况出现,你需要使用所有这些类型的演员。

I've been out of the loop with C++ for a few years now and it seems like every time I turn around they add a few new keywords. I was recently introduced to C++'s various casting operators reinterpret_cast, static_cast, dynamic_cast, and const_cast. I'm a little fuzzy on exactly when those situations come up that you would need to use all these types of casts.

我一直使用Objective-C一两年了,感觉很舒服。 (直到那时为止大多是一个C人。我试图理解为什么C ++似乎有这么复杂。或者换句话说,Objective-C缺少什么似乎没有(或需要?)这种铸造类型?

I've been using Objective-C for a year or two now and feel fairly comfortable with it. (Been mostly a C person until then). I'm trying to understand why C++ seems to have all this complexity. Or to put it another way, what's Objective-C missing that it doesn't seem to have (or need?) this many casting types?

推荐答案

See this answer to the question When should static_cast, dynamic_cast and reinterpret_cast be used? on the meaning of each kind of casts.


什么是目标C缺少它似乎没有(或需要?)这种铸造类型?

what's Objective-C missing that it doesn't seem to have (or need?) this many casting types?

C ++更侧重于类型安全比C. 添加了许多演员操作员来制作许多不同的演员意图明确(并且阻止人们由于其丑陋的形式使用它)。

C++ focuses a lot more in type safety than C. The many cast operators are added to make the many different casting intentions clear (and to discourage people from using it due to its ugly form). And,


  • 没有const对象( const NSObject * )在Objective-C中,和其他const参数不像C ++那样强调,因此 const_cast 是无用的。

  • There is no const objects (const NSObject*) in Objective-C, and other const parameters aren't so emphasized unlike in C++, so const_cast is useless.

Objective-C实例总是使用动态类型,因此不需要 dynamic_cast 。 (ObjC中的类型检查通常使用 -isKindOfClass:。)

Objective-C instances always use dynamic typing, so dynamic_cast is not needed. (Type checking in ObjC is usually done with -isKindOfClass:.)

static_cast reinterpret_cast 在C中相同,但在C ++中不同。因为C ++支持多重继承(在ObjC中缺失),一个指针转换不如无操作那么简单:

static_cast and reinterpret_cast are the same in C, but not so in C++. Because C++ supports multiple inheritance (missing in ObjC), a pointer casting is not as simple as a 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
}


这篇关于Objective-C和C ++之间的转换比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆