The following precautions must be followed when rewriting functions: The function signature is the same. Access modifiers cannot be stricter than those of the base class. Use const or override marks for rewriting. Only virtual functions can be rewritten. Traps include hiding base class functions and multiple rewritings. and no intention of rewriting. Use overrides correctly and consider the pitfalls to avoid unexpected behavior.
C Precautions and pitfalls of function rewriting
Function rewriting, in object-oriented programming, refers to subclassing The ability to override a function of the same name in a base class. It's a powerful mechanism, but one that can come with pitfalls if used incorrectly.
Notes:
-
#Function signature must be the same:The overridden function must have the same parameter list and the same parameter list as the base class function Return type.
-
Access modifiers cannot be more strict: The access modifiers of subclass functions cannot be more strict than those of base class functions. For example, if a base class function is public, a subclass function cannot be protected or private.
-
const or override: Use the const or override keyword to clearly indicate that the function is to override the base class function instead of adding a new function.
-
Virtual function: Only virtual functions can be overridden. A base class function can be declared as virtual by using the virtual keyword.
Trap:
-
Hide base class function: If the signature of the subclass function is the same as the base class function, but The access modifier is more permissive and hides the base class function instead of overriding it.
-
Multiple overrides: A function can only be overridden in a single subclass. If multiple subclasses try to override the same function, the compiler will report an error.
-
Unintentional override: If a subclass function happens to have the same signature as a base class function, but is not actually intended to override it, this can lead to unexpected behavior.
Practical case:
Consider the following base class and subclass example:
class Base {
public:
virtual void print() const {
std::cout << "Base::print() called" << std::endl;
}
};
class Derived : public Base {
public:
void print() const override {
std::cout << "Derived::print() called" << std::endl;
}
};
Here, the Derived class overrides the Base class The print() function, the subclass function correctly overrides the base class function and outputs a different message.
Tips to avoid pitfalls:
- Explicitly use const or override to indicate overriding.
- Check whether the access modifier is correct.
- Make sure to only override functions if you intend to override them.
- Check function signatures carefully to avoid accidental overrides.
The above is the detailed content of Notes and pitfalls of C++ function rewriting. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn