c++ - Objective-C有虚函数的概念吗?
天蓬老师
天蓬老师 2017-04-17 11:17:16
0
2
630

看到有说是代理:

  • @optional - 虚函数
  • @required - 纯虚函数

也有说OC中所有的函数都是虚函数,求真相!

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
大家讲道理

I have written a related blog before http://www.cnblogs.com/sinkup/p/3496810.html. It is relatively superficial. If you are interested, you can take a look^_^
As for the proxy (protocol), it is equivalent to the class used for multiple inheritance...

I think the concept is different, but the nature is similar.

小葫芦

To say that a proxy is a virtual function does not make it clear at all what the meaning of virtual functions in C++ is. Virtual functions are the basis for polymorphism in C++. The running system will automatically select the appropriate specific implementation to run according to the type of object. Methods that are not designated as virtual functions will not have this property.

A proxy is a kind of delegation, which transfers part of the function to another object for implementation. It is essentially the delegation mode in the design pattern, which is simpler than the proxy mode.

It is said that all functions in OC are virtual functions, which can also be understood this way, because the message mechanism of OC has a call based on the nearest principle. If a cache method is found, the method of this class will be called first. When the method of the subclass exists, the method of the subclass will be called in a limited manner.

C++ generates machine code, so C++ needs virtual functions to declare methods, and then the compiler compiles the virtual base table of the function (essentially a proxy to find the actual address of the function) to achieve polymorphism. Such an implementation will also bring considerable performance loss. The OC is more dynamic, and of course the performance loss is also more severe. However, this performance loss brings more flexibility and simpler implementation, which improves development efficiency.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template