zoukankan      html  css  js  c++  java
  • covariant return type in override

     1 #include <iostream>
     2  
     3 class Base
     4 {
     5 public:
     6     // This version of getThis() returns a pointer to a Base class
     7     virtual Base* getThis() { std::cout << "called Base::getThis()
    "; return this; }
     8     void printType() { std::cout << "returned a Base
    "; }
     9 };
    10  
    11 class Derived : public Base
    12 {
    13 public:
    14     // Normally override functions have to return objects of the same type as the base function
    15     // However, because Derived is derived from Base, it's okay to return Derived* instead of Base*
    16     virtual Derived* getThis() { std::cout << "called Derived::getThis()
    ";  return this; }
    17     void printType() { std::cout << "returned a Derived
    "; }
    18 };
    19  
    20 int main()
    21 {
    22     Derived d;
    23     Base *b = &d;
    24     d.getThis()->printType(); // calls Derived::getThis(), returns a Derived*, calls Derived::printType
    25     b->getThis()->printType(); // calls Derived::getThis(), returns a Base*, calls Base::printType
    26 }

     Note that some older compilers (e.g. Visual Studio 6) do not support covariant return types.

     One interesting note about covariant return types: C++ can’t dynamically select types, so you’ll always get the type that matches the base version of the function being called.

     In the above example, we first call d.getThis(). Since d is a Derived, this calls Derived::getThis(), which returns a Derived*. This Derived* is then used to call non-virtual function Derived::printType().
     Now the interesting case. We then call b->getThis(). Variable b is a Base pointer to a Derived object. Base::getThis() is virtual function, so this calls Derived::getThis(). Although Derived::getThis() returns a Derived*, because base version of the function returns a Base*, the returned Derived* is downcast to a Base*. And thus, Base::printType() is called.

     In other words, in the above example, you only get a Derived* if you call getThis() with an object that is typed as a Derived object in the first place.

     do not use virtual function in construction and destruction.

  • 相关阅读:
    kill一个pthread_test.bin测试程序主线程、子线程退出kernel flow
    signal bit operation
    pthread
    信号发送处理流程
    sdcardfs
    node小贴士03
    node小贴士02
    node小贴士01
    siteserver cms 搜索功能
    语法的高亮显示
  • 原文地址:https://www.cnblogs.com/yetanghanCpp/p/9282749.html
Copyright © 2011-2022 走看看