调用继承的方法会导致警告而不转换为超类
我有以下继承树:
NSObject <- GameObject <- RenderableObject <- SimpleBullet
GameObject
有一个名为 bounds
的方法,它返回对象的边界。在我的代码中,我有一个 SimpleBullet
调用 bounds
来获取其边界,并且我收到一条警告,指出 bounds
已在多个位置定义;奇怪的。如果我将 SimpleBullet
转换为 GameObject
并调用 bounds
方法,一切都会按预期进行。发生什么事了?我无法弄清楚这种行为。示例:
SimpleBullet* bullet = bulletInstance;
[bullet bounds]; // we get the warning.
[(GameObject*)bullet bounds]; // works as expected.
正如我所说,bounds
方法是在 GameObject
中定义的,但为什么 Obj-C 不知道 SimpleBullet
是一个 GameObject
并且不允许我在没有警告的情况下调用它的方法?
I have the following inheritance tree:
NSObject <- GameObject <- RenderableObject <- SimpleBullet
GameObject
has a method called bounds
that returns the object's bounds. In my code I have a SimpleBullet
that calls bounds
to get its bounds, and I receive a warning saying bounds
is defined in several places; odd. If I cast the SimpleBullet
to a GameObject
and call the bounds
method, everything works as expected. What's happening? I can't figure out this behaviour. Example:
SimpleBullet* bullet = bulletInstance;
[bullet bounds]; // we get the warning.
[(GameObject*)bullet bounds]; // works as expected.
As I said, the bounds
method is defined in GameObject
, but why is Obj-C not aware that SimpleBullet
is a GameObject
and not allowing me to call its method without the warning?
经过一番调查,我终于知道发生了什么事。编译器在捉弄我,因为没有报告有意义的错误。问题是我没有导入/包括 SimpleBullet.h。声明了前瞻性声明,但没有真正实施。
这让我认为编译器默认将其视为 id 对象,并尝试在所有注册的类中找到适合调用签名的方法。只是猜测,对此不确定:)。
感谢您的所有帮助。
After some investigation I managed to know what was happening. The compiler was playing tricks on me because did not report a meanfull error. The problem was that I wasn't importing/including SimpleBullet.h. A forward declaration was declared but not the real implementation.
This, leads me to think that by default compiler treats it as an id object, and tries to find a method among all registered classes that suits the call signature. Just a guess, not sure about this :).
Thanks for all the help.