三重嵌套类中的协变返回类型无效

时间:2014-04-14 20:16:55

标签: c++ oop inheritance

运行此代码时出现错误:

  

prog.cpp:36:25:错误:'virtual const EEE :: N :: D * EEE :: N :: d()const'

的无效协变返回类型
   virtual const D *d() const { return 0; }
                     ^
     

prog.cpp:24:25:错误:覆盖'virtual const EE :: N :: D * EE :: N :: d()const'

   virtual const D *d() const{ return 0; }
#include <iostream>
using namespace std;

class E {
public:
    class N {
    public:
       class D{
       public:
           D() {}
       };
       virtual const D *d() const{ return 0; }
    };
};

class EE : public E {
public:
   class N : public E::N {
   public:
       class D : public E::N::D {
       public:
           D() : E::N::D()  {}
       };
       virtual const D *d() const{ return 0; }   // No error here
   };
};

class EEE : public EE {
public:
   class N : public EE::N {
   public:
       class D : EE::N::D {
       public:
          D() : EE::N::D()  { }
       };
       virtual const D *d() const { return 0; }  // Error here  why?
   };
};

int main()
{
    return 0;
}

0 个答案:

没有答案