gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:35:51 31 4
gpt4 key购买 nike

在 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++ - 协变 cv 限定符是否适用于 C++ 中的原始类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22502653/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com