协变cv限定符是否适用于C中的原始类型?

前端之家收集整理的这篇文章主要介绍了协变cv限定符是否适用于C中的原始类型?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在C中,减少派生类中返回类型的cv限定符是有效的:
class Base {
    virtual const Base* f();
};

class Derived : public Base {
    Base* f() override;
};

这对指向原始类型的指针有效吗?

class Base {
    virtual const int* f();
};

class Derived : public Base {
    int* f() override;
};

解决方法

协变cv限定符是否适用于C中的原始类型?

没有

§ 10.3.7 Virtual functions

The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions. If a function D::f overrides a function B::f,the return types of the functions are covariant if they satisfy the following criteria:

  • both are pointers to classes,both are lvalue references to
    classes,or both are rvalue references to classes
  • the class in the return type of B::f is the same class as the class
    in the return type of D::f,or is an unambiguous and accessible
    direct or indirect base class of the class in the return type of D::f
  • both pointers or references have the same cv-qualification and the
    class type in the return type of D::f

猜你在找的C&C++相关文章