“Invalid application of 'sizeof' to interface 'Fraction' in non-fragile ABI” in Objective-C

。_饼干妹妹 提交于 2019-11-27 08:16:27

问题


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:

Invalid application of 'sizeof' to interface 'Fraction' in non-fragile ABI

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).

What am I doing wrong?


回答1:


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.




回答2:


To follow up on what Chuck said, you should use class_getInstanceSize() at runtime if you really need to know the size of an object.



来源:https://stackoverflow.com/questions/7219319/invalid-application-of-sizeof-to-interface-fraction-in-non-fragile-abi-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!