Clean C ++: privatization override virtual functions

In C++11added to overridethe keyword, when subclass needs override base class virtual function, to provide explicit override, the compiler can improve safety.

Compile-time safety

For example, the presence of a base class Testthat declares 2abstract methods.

struct TestResult;

struct Test {
  virtual void run(TestResult&) = 0;
  virtual int countTestCases() const = 0;

  virtual ~Test() {}
};

Abstract base class method override in the subclass explicitly denoted overridenot only enhance readability, but also enhance the safety during compilation. If you strictly follow the rules, when the reconstruction method signature abstract base class, the compiler can find the reference point for all fail to compile accurately, providing an excellent reconstruction of the protective effect.

#include "cut/core/test.h"

struct TestDecorator : Test { 
  TestDecorator(Test& test);

private: 
  void run(TestResult&) override;
  int countTestCases() const override;

private:
  Test& test; 
};

Press programming interfaces

"Press the programming interface" is an important object-oriented principles. Following this principle, it can be done between the decoupling module, such that a change in the two directions remain independent. In the C ++ language, and access control override polymorphic behavior is isolated, subclasses can override the parent private virtual functions. For example, a typical implementation mode of the template method, are private override base class virtual function implemented.

Privatization override virtual functions, can effectively ensure that users mistakenly call a member of the subclass, and clearly tell the user program should be implemented based on the abstract interface type. For example, the embodiment TestDecoratorconstructor must be public, or instance of that type would not be constructed. Overwrites all virtual functions are declared private, the user should be based on alert Testat runtime calls the corresponding abstract methods polymorphically.

TestSuite: Test set an example of realization of warehouse

With TestSuite, for example, it held a series of Testexamples of the type of their run-time type may be TestCase, TestSuite, TestDecoratormore. Based on the abstract Testtype, with a tree structure to achieve implicit set of embodiments.

#include "cut/core/test.h"
#include "cut/core/internal/bare_test_suite.h"
#include <vector>

struct TestSuite : Test, private BareTestSuite {
  ~TestSuite();

  void add(Test* test);

private:
  void run(TestResult& result) override;
  int countTestCases() const override;

private:
  const Test& get() const override;
  void runBare(TestResult& result) override;

private:
  std::vector<Test*> tests;
};

TestSuiteWhen implementing destructor and other functions, it is based on Testthe abstract type of multi-state operation when invoked.

#include "cut/core/test_suite.h"
#include "cub/base/algo.h"

void TestSuite::add(Test* test) {
  tests.push_back(test);
}

TestSuite::~TestSuite() {
  for (auto test : tests) {
    delete test;
  }
}

int TestSuite::countTestCases() const {
  static auto accumulator = [](Test* test){
    return test->countTestCases();
  };
  return cub::reduce(tests, 0, accumulator);
}

const Test& TestSuite::get() const {
  return *this;
}

void TestSuite::runBare(TestResult& result) {
  for (auto test : tests) {
    test->run(result);
  }
}

void TestSuite::run(TestResult& result) {
  result.runTestSuite(*this);
}

Test, TestSuite, TestCase, TestDecoratorThe relationship between as shown below.

2254249-c1d6e92c2f475d00.jpg
Implicit tree: in combination with the modified

Guess you like

Origin blog.csdn.net/weixin_34417183/article/details/90959638