leveldb source code, Why db_impl.cc implement pure virtual function "DB::Delete"

StackOverflow https://stackoverflow.com/questions/18249511

  •  24-06-2022
  •  | 
  •  

سؤال

Here is the stat and implement:

stat: virtual Status Delete(const WriteOptions& options, const Slice& key) = 0;

implement: Status DB::Delete(const WriteOptions& opt, const Slice& key) { WriteBatch batch; batch.Delete(key); return Write(opt, &batch); }

هل كانت مفيدة؟

المحلول

The Delete in the base class might be incomplete. The implementation provides all what the base class can do, though. Hence a derived class should call Base::Delete. Without proper documentation programmers might miss that.

In the standard at 10.3: "A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both;"

This compiles (g++ -std=c++11 test.cc -o test) and runs happily:

#include <iostream>
struct X { virtual void f() = 0; };
struct Y : X { virtual void f(); };
void X::f () { std::cout << 'X' << std::endl;}
void Y::f () { X::f(); std::cout << 'Y' << std::endl;}
int main() {
    Y y;
    y.f();
    return 0;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top