Is it possible to force Objective-C to call a specific instance of a virtual method, rather than going through the standard virtual message dispatch? I know this is generally a Bad Idea, but I’d like to know how to do it using the Objective-C runtime.
For example, given class A and B that implement -(void) foo, where B is a subclass of A, I’d like to call the foo method on A with the B instance (even though B would normally handle this message).
I know that I can make this happen by moving the guts of A’s foo method to a new method and delegating to it, but I’d like to figure out some way to do this through the Objective-C runtime.
NOTE: For the purposes of this question, assume that I can’t change the source of A or B and I’ve carefully weighed the risks of breaking encapsulation.
This page is a great source for understanding the runtime; a quick memory-assisted scan shows that the section titled “So what happens in objc_msgSend anyway?” is a good place to start for an immediate answer, but the article as a whole will really help you understand what goes on.
Here’s an example where he queries the runtime for the appropriate function pointer, then calls the function directly: