zoukankan      html  css  js  c++  java
  • Overriding the Virtual Mechanism

    In C++, dynamic binding happens when a virtual function is called through a reference(or a pointer) to a base class.
     
    Calls to virtual functions made through a reference( or a pointer )are resolved at run time.
     
    Derived class and virtual functions
     
    The declaration of a virtual function in the derived class must exactly match the way the function is defined in the base.
     
    But there is one exception that virtuals could return a reference or pointer to a type that is itself a bass class. A virtual
     
    function in a derived class can return a reference or a pointer to a class that is publicly derived from the type returned by
     
    the bass class function.
     
    Note:
     
    Once a function is declared as virtual in a base class it remains virtual;
     
    nothing the derived class do can change the fact that the function is
     
    virtual.  When a derived class redefines a virtual, it may use the virtual
     
    keyword, but it is not required to do so.
     
    A class must be defined to be used as a base class
     
    One implication of this rule is that it is impossible to derived a class from itself.
     
    Overriding the virtual mechanism
     
    In some cases, we want to overide the virtual mechanism and force a call to use a particular
     
    version of a virtual function. We can do so by using the scope operator.
     
    Best practise: only code inside member functions should ever need to use the scope
     
    operator to override the virtual mechanism.
     
    Attention: When a derived virtual calls the base-class version, it must do so explicitly
     
    using the scope operator. If the derived function neglected to do so, then the call would
     
    be resolved at run time and would be a call to itself resulting in an infinite recursion.
     
    A virtual function can have default arguments:
     
    As usual, the value, if any, on default argument used in a given call is determined at compile time.
     
    So, the value that is used is the one defined by the type through which the function is called, irrespective
     
    of the object's dynamic type.
     
    Using different default arguments in the base and derived versions of the same virtual is almost
     
    guaranteed to cause trouble.
     
    Key concept: inheritance versus composition
     
    When we define one class as publicly inherited from another, the derived class should reflect a so-called
     
    "Is A" relationship to the base class.
     
    Another common relationship among types is a so-called "Has A" relationship.
     
    Exempting individual members in inheritance:
     
    Just as we can use a using declaration to use names from the std namespace, it may also use a using declaration
     
    to access a name from a base class.
     
    Inheritance and Friendship
     
    Friendship is not inherited.
     
    Inheritance and Static Members
     
    If a base class defines a static member there is only one such member defined for the entire hieraichy.
     
    Assuming the member is accessable, we can access the static member either through the base or derived
     
    class. As usual, we can either use scope operator or the dot or arrow member access operators.
  • 相关阅读:
    敏捷开发第五天
    敏捷开发第四天
    系统用户分析模型
    第三天敏捷开发
    第二天敏捷开发
    敏捷开发第一天
    第三周学习总结
    [学习笔记]莫队算法
    【网络流】Modular Production Line
    [学习笔记]set的使用
  • 原文地址:https://www.cnblogs.com/cavehubiao/p/4913821.html
Copyright © 2011-2022 走看看