开发者

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

开发者 https://www.devze.com 2023-03-31 01:34 出处:网络
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

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开发者_Python百科'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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消