本文介绍了“将'sizeof'无效应用到非脆弱ABI中的'Fraction'接口"在Objective-C中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Steven Kochan的"Objective-C 2.0编程".我们创建了一个带有两个int实例变量的Fraction对象.在本书的后面,Kochan在一个分数对象的指针myFract上使用了sizeof语句:

I'm studying Steven Kochan's "Programming in Objective-C 2.0". We created a Fraction object with two int instance variables. Later in the book Kochan uses the sizeof statement on a Fraction object's pointer myFract:

sizeof(*myFract)

执行此操作时,出现编译错误:

When I do this, I receive a compile error:

http://clang.llvm.org/compatibility.html#sizeof-interface 指出,该对象的大小可以更改,但Fraction实例仅包含两个int实例变量(加上书中提到的继承的isa成员")时,可能会发生此错误.

http://clang.llvm.org/compatibility.html#sizeof-interface states this error could occur for an object who's size can change but a Fraction instance only contains the two int instance variables (plus an "inherited isa member" mentioned in the book).

我在做什么错了?

推荐答案

由于您使用的是现代ABI,因此对象的大小仍然可以更改.在旧版本的Objective-C中,对象基本上是结构,这意味着可以对它们进行sizeof()处理.情况已不再如此,从一开始它就从来不是一个特别好的主意.我不确定Kochan打算教什么,但是仅供参考,这对于编写Objective-C来说不是必需的.通过在Mac上将其构建为32位,您应该能够获得旧的行为,但是同样,在真正的程序中,这不是您想要做的.

The object's size could still change because you're using the modern ABI. In older versions of Objective-C, objects were basically structs and that meant it was possible to sizeof() them. This is no longer the case, and it was never a particularly good idea in the first place. I'm not sure what Kochan was trying to teach with it, but FYI this is not necessary to program Objective-C. You should be able to get the old behavior by building as 32-bit on the Mac, but again, that isn't something you'll want to do in real programs.

这篇关于“将'sizeof'无效应用到非脆弱ABI中的'Fraction'接口"在Objective-C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:47