In C++ it is valid to reduce the cv-qualifier of a return type in a derived class:
class Base {
virtual const Base* f();
};
class Derived : public Base {
Base* f() override;
};
Is this valid with pointers to primitive types?
class Base {
virtual const int* f();
};
class Derived : public Base {
int* f() override;
};
Do covariant cv-qualifiers apply to primitive types in C++?
NO
§ 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 functionB::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 ofD::f
, or is an unambiguous and accessible direct or indirect base class of the class in the return type ofD::f
- both pointers or references have the same cv-qualification and the class type in the return type of
D::f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With