Вопрос по inheritance, c++ – Почему C ++ не позволяет базовым классам реализовывать наследуемый интерфейс производного класса?

13

Error: User Rate Limit Exceeded

// some guy wrote this, used as a Policy with templates
struct MyWriter {
  void write(std::vector<char> const& data) {
    // ...
  }
};

В каком-то существующем коде люди использовали не шаблоны, а интерфейсы + стирание типов

class IWriter {
public:
  virtual ~IWriter() {}

public:
  virtual void write(std::vector<char> const& data) = 0;
};

Error: User Rate Limit Exceeded

class MyOwnClass: private MyWriter, public IWriter {
  // other stuff
};

Error: User Rate Limit Exceeded

class MyOwnClass: private MyWriter, public IWriter {
public:
  void write(std::vector<char> const& data) {
    MyWriter::write(data);
  }
};

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded SigTerm
Error: User Rate Limit ExceededstructError: User Rate Limit Exceededinterface? Bo Persson
Error: User Rate Limit ExceededvirtualError: User Rate Limit Exceeded Ben Voigt
Error: User Rate Limit ExceededheError: User Rate Limit Exceeded Konrad Rudolph
Error: User Rate Limit Exceeded SigTerm

Ваш Ответ

5   ответов
-2

Error: User Rate Limit Exceeded

5

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Solution #1Error: User Rate Limit Exceeded

Soluion #2Error: User Rate Limit Exceeded

Compiler must ensure that MyWriter::write does what IWriter::write() is supposed to do. Compiler must ensure that calling MyWriter::write from IWriter will not break existing functionality in MyWriter code programmer expects to use elsewhere.

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededaskedError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Johannes Schaub - litb
Error: User Rate Limit Exceededstruct A { void nonVirtualNastiness() {} virtual void bark() { } };Error: User Rate Limit ExceededAError: User Rate Limit Exceededstruct A : public IDoggy { virtual void bark() { } };Error: User Rate Limit ExceededIDoggy::barkError: User Rate Limit ExceededA::barkError: User Rate Limit ExceedednonVirtualNastinessError: User Rate Limit Exceeded Johannes Schaub - litb
Error: User Rate Limit Exceeded
3

Error: User Rate Limit ExceededCouldError: User Rate Limit Exceeded

Error: User Rate Limit ExceededMyWriterError: User Rate Limit ExceededIWriterError: User Rate Limit ExceededMyWriterError: User Rate Limit ExceededIWriterError: User Rate Limit ExceededMyWriter*Error: User Rate Limit ExceededIWriter*Error: User Rate Limit Exceededvoid (MyWriter::*)(const std::vector<char>&)Error: User Rate Limit Exceededvoid (IWriter::*)(const std::vector<char>&).

Error: User Rate Limit ExceededcouldError: User Rate Limit Exceeded

Error: User Rate Limit ExceededthisError: User Rate Limit Exceededvoid (MyWriter::*)(const std::vector<char>&)Error: User Rate Limit ExceededIWriter*Error: User Rate Limit Exceededhave this methodError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededIWriter*Error: User Rate Limit ExceededwriteError: User Rate Limit ExceededIWriter*Error: User Rate Limit ExceededMyWriter::writerError: User Rate Limit ExceededMyWriter::writer needsError: User Rate Limit ExceededMyWriterError: User Rate Limit ExceededIWriterError: User Rate Limit ExceededMyWriter.

Error: User Rate Limit ExceededIWriterError: User Rate Limit Exceeded

Error: User Rate Limit ExceededeveryError: User Rate Limit Exceeded

Error: User Rate Limit ExceededneverError: User Rate Limit Exceeded

Error: User Rate Limit ExceedednotError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededMyWriterError: User Rate Limit Exceededdynamic_castError: User Rate Limit Exceeded
Error: User Rate Limit ExceededAError: User Rate Limit ExceededBError: User Rate Limit Exceededstruct A { virtual ~A() {}}; struct B { virtual ~B() {} void f() {}}; struct C : A, B {}; int main() { A *a = new C; dynamic_cast<B*>(a)->f(); }Error: User Rate Limit Exceeded Johannes Schaub - litb
Error: User Rate Limit ExceededsomeError: User Rate Limit Exceeded
Error: User Rate Limit ExceededMyWriter::writeError: User Rate Limit ExceededIWriter::writeError: User Rate Limit ExceededMyOwnClassError: User Rate Limit ExceededMyWriter::writeError: User Rate Limit ExceededIWriter::write ends upError: User Rate Limit ExceededMyWriter::writeError: User Rate Limit ExceededMyWriter::write. Johannes Schaub - litb
15

both of which have implementation

writeMyBigClass

If write is only called through base class pointers, then defining write in the derived class is NOT necessary, contrary to the claim in the question.

MyWriter::writeMyBigClassIWriter* const thisMyWriter::writeMyWriter* const thisIWriterMyWriter

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Johannes Schaub - litb
Error: User Rate Limit Exceededlanguage designError: User Rate Limit Exceeded Johannes Schaub - litb
Error: User Rate Limit Exceededvoid write(IMyWriter* const this, std::vector<char> const&)Error: User Rate Limit ExceededthisError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
4

automaticallyMyOwnClassIWriterMyWriter

class MyOwnClass: private MyWriter, public IWriter {
public:
  void write(std::vector<char> const& data) final = MyWriter::write;
};

MyWriterfinalvirtual

Похожие вопросы