search

Home  >  Q&A  >  body text

c++ - Objective-C有虚函数的概念吗?

看到有说是代理:

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

天蓬老师天蓬老师2805 days ago634

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 11:19:16

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 11:19:16

    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.

    reply
    0
  • Cancelreply