Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of a C++ pure-virtual function in Objective-C?

Simple answer would be Protocol.

The another point is that it is said that all methods in ObjectC are virtual, so no need to say virtual in ObjC.

I find it hard to understand this concept.

Any comments to figure out more clear around this question ?

Thanks for commenting.

like image 280
Forrest Avatar asked Dec 07 '10 08:12

Forrest


People also ask

What is a pure virtual function in C?

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.

Can we call pure virtual functions using object?

Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined.

What is virtual and pure virtual function in C?

A virtual function is a member function of base class which can be redefined by derived class. A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract.

What is pure virtual function in SV?

A pure virtual function is a member of an "abstract" base class. You cannot create an object of an abstract class. No implementation need be provided for the pure virtual function in the base class but it must be overridden in a derived class if you want to create objects of that type.


1 Answers

Simple answer would be Protocol.

Simple but wrong. A protocol is an interface specification. It's a collection of messages that an object must (ignoring the @optional keyword for now) respond to.

The term "virtual function" has no direct counterpart in Objective-C. In Objective-C you don't call functions on objects, you send messages to them. The object itself then decides how to respond to the message, typically by looking up the message in its class object, finding the associated method and invoking it. Note that this all happens at run time, not compile time.

The mapping between messages (or "selectors" to give them their technical term) and methods is built entirely from the @implementation. The method declarations in the @interface are only there to give the compiler the information it needs to warn you that you may have forgotten a method implementation. And it is only a warning because you can't tell until run time whether whether the object really does respond to the message or not. For example, somebody else could add a category to an existing class that provides implementations for missing methods, or a class could override forwardingTargetForSelector: to forward messages it doesn't respond to elsewhere.

like image 189
JeremyP Avatar answered Nov 15 '22 18:11

JeremyP