0

Before I start let me ask that in objective-c 2.0 you can have a baseclass pointer reference a subclass object?

If so is there something similar to c++ virtual member functions in objective-c.

For instance if a subclass object gets called from a baseclass pointer, will it call the subclass method (it properly overrides the base class method)? Would it be forced as well like Java or does the programmer have control over it?

Edit: would it be possible to assign any pointer type to another, what is the limit? Ex: Can you say

Subclass *s = ... Baseclass *b= s

Or can it only be done by allocation?

rubixibuc
  • 7,111
  • 18
  • 59
  • 98

1 Answers1

1

You can have a base class pointer hold onto the value of a subclass instance's address. So

Subclass s* = /* get an object instance */
Baseclass b* = s;

is possible (this doesn't have to be during init). There isn't a limit.

The "virtual method" like functionality is like Java, so every method is virtual so you don't have control over it (e.g. the subclass method is always called). Messages being sent to objects have, in effect, late binding.

See Implement a pure virtual method in Objective-C .

Community
  • 1
  • 1
Bryant Luk
  • 2,448
  • 21
  • 22