In C++11
added to override
the 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 Test
that declares 2
abstract 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 override
not 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 TestDecorator
constructor 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 Test
at 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 Test
examples of the type of their run-time type may be TestCase, TestSuite, TestDecorator
more. Based on the abstract Test
type, 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;
};
TestSuite
When implementing destructor and other functions, it is based on Test
the 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, TestDecorator
The relationship between as shown below.