-
final 指定符 (C++11 起)
在C++11中关键字final,用来指定派生类不能覆写虚函数,或类不能被继承。应用到成员函数时,标识符final 在类定义中的成员函数声明或成员函数定义的语法中,立即出现于声明器后。应用到类时,标识符 final
出现在类定义的起始,立即在类名后。例如下面leveldb代码中,类PosixLogger不能被其他的类继承了
namespace leveldb {
class PosixLogger final : public Logger {
public:
// Creates a logger that writes to the given file.
//
// The PosixLogger instance takes ownership of the file handle.
explicit PosixLogger(std::FILE* fp) : fp_(fp) {
assert(fp != nullptr);
}
~PosixLogger() override {
std::fclose(fp_);
}
.......
.......
.......
private:
std::FILE* const fp_;
};
} // namespace leveldb
#endif // STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
-
override 指定符(C++11 起)
在C++11中关键字override,它用来指定基类中的虚成员函数需要在子类中显式地重写,如果没有重写则编译器报错。例如在上面代码中就有关键字override的使用
-
default 指定符
在C++11中关键字default,它用来显式默认化的函数定义,令编译器为类生成特殊成员函数或比较运算符 (C++20 起)的显式指令。其中特殊成员函数有:默认构造函数、复制构造函数、移动构造函数 (C++11 起)、复制赋值运算符、移动赋值运算符 (C++11 起)、析构函数。即,如果某个特殊成员函数在声明时使用了default,那么编译器会为该函数自动生成函数体。例如leveldb代码
namespace leveldb
{
class LEVELDB_EXPORT Cache;
// Create a new cache with a fixed size capacity. This implementation
// of Cache uses a least-recently-used eviction policy.
LEVELDB_EXPORT Cache* NewLRUCache(size_t capacity);
class LEVELDB_EXPORT Cache {
public:
Cache() = default;
Cache(const Cache&) = delete;
Cache& operator=(const Cache&) = delete;
// Destroys all existing entries by calling the "deleter"
// function that was passed to the constructor.
virtual ~Cache();
// Opaque handle to an entry stored in the cache.
struct Handle { };
......
......
}//class Cache
}//namespace leveldb
关于cache的定义时,使用了该关键字。
-
delete 指定符
在C++11中关键字delete,它的定义与default是一样的,但是功能不一样。delete指示禁止使用特殊成员函数。在上面的代码中使用到了关键字delete