- 重载:overload
- 重写:override
- 重定义:redefine
final
Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be derived from.
#include <iostream>
struct A
{
virtual void foo();
void bar();
virtual ~A();
};
// member functions definitions of struct A:
void A::foo() { std::cout << "A::foo();
"; }
A::~A() { std::cout << "A::~A();
"; }
struct B : A
{
// void foo() const override; // Error: B::foo does not override A::foo
// (signature mismatch)
void foo() override; // OK: B::foo overrides A::foo
// void bar() override; // Error: A::bar is not virtual
~B() override; // OK: `override` can also be applied to virtual
// special member functions, e.g. destructors
void override(); // OK, member function name, not a reserved keyword
};
// member functions definitions of struct B:
void B::foo() { std::cout << "B::foo();
"; }
B::~B() { std::cout << "B::~B();
"; }
void B::override() { std::cout << "B::override();
"; }
int main() {
B b;
b.foo();
b.override(); // OK, invokes the member function `override()`
int override{42}; // OK, defines an integer variable
std::cout << "override: " << override <<
;
}
override
Specifies that a virtual function overrides another virtual function.
#include <iostream>
struct A
{
virtual void foo();
void bar();
virtual ~A();
};
// member functions definitions of struct A:
void A::foo() { std::cout << "A::foo();
"; }
A::~A() { std::cout << "A::~A();
"; }
struct B : A
{
// void foo() const override; // Error: B::foo does not override A::foo
// (signature mismatch)
void foo() override; // OK: B::foo overrides A::foo
// void bar() override; // Error: A::bar is not virtual
~B() override; // OK: `override` can also be applied to virtual
// special member functions, e.g. destructors
void override(); // OK, member function name, not a reserved keyword
};
// member functions definitions of struct B:
void B::foo() { std::cout << "B::foo();
"; }
B::~B() { std::cout << "B::~B();
"; }
void B::override() { std::cout << "B::override();
"; }
int main() {
B b;
b.foo();
b.override(); // OK, invokes the member function `override()`
int override{42}; // OK, defines an integer variable
std::cout << "override: " << override <<
;
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END

















暂无评论内容