Covariant cv qualifiers apply to primitive types in C ++?

In C ++, it is permissible to reduce the cv qualifier of the return type in a derived class:

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

Is this true with pointers to primitive types?

 class Base { virtual const int* f(); }; class Derived : public Base { int* f() override; }; 
+7
c ++ covariance const
source share
1 answer

Are covariance cv qualifiers used for primitive types in C ++?

NOT

Β§ 10.3.7 Virtual functions

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

  • both are pointers to classes, both are lvalue values ​​for classes, or both are rvalue values ​​for classes
  • the class of the return type B::f is the same class as the class in the return type D::f , or is the unambiguous and accessible direct or indirect base class of the class in the return type D::f
  • both pointers or references have the same cv qualification and class type in the return type D::f
+6
source share

All Articles