book: translate ch02

Update #12
This commit is contained in:
Changkun Ou
2019-07-20 13:34:18 +02:00
parent 2eb8fdb099
commit b12e03e82d
4 changed files with 1162 additions and 39 deletions

View File

@@ -1,11 +1,11 @@
//
// 2.7.cpp
// 2.19.constructor.cpp
// chapter 2 language usability
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
// 面向对象增强
#include <iostream>
#include <string>
@@ -18,12 +18,12 @@ public:
str = s;
}
// 委托构造
// delegate constructor
Base(std::string s, int v) : Base(s) {
value = v;
}
// 终止重载
// final constructor
virtual void foo() final {
return;
}
@@ -36,26 +36,26 @@ public:
double floating;
Subclass() = delete;
// 继承构造
// inherit constructor
Subclass(double f, int v, std::string s) : Base(s, v) {
floating = f;
}
// 显式重载
// explifict constructor
virtual void foo(int v) override {
std::cout << v << std::endl;
value = v;
}
}; // 合法 final
}; // legal final
// class Subclass2 : Subclass {
// }; // 非法, Subclass final
// }; // illegal, Subclass has final
// class Subclass3 : Base {
// void foo(); // 非法, foo final
// void foo(); // illegal, foo has final
// }
int main() {
// Subclass oops; // 非法, 默认构造已删除
// Subclass oops; // illegal, default constructor has deleted
Subclass s(1.2, 3, "abc");
s.foo(1);

View File

@@ -1,11 +1,10 @@
//
// 2.8.cpp
// 2.20.strong.type.enum.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
// 强类型枚举
#include <iostream>
template<typename T>
@@ -14,7 +13,7 @@ std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::os
return stream << static_cast<typename std::underlying_type<T>::type>(e);
}
// 如果两个都定义为 value1 和 value2,将引发重定义错误
// there will be compile error if all define value1 和 value2
enum Left {
left_value1 = 1,
left_value2
@@ -37,8 +36,8 @@ int main() {
std::cout << "Left::value1 == Right::value2" << std::endl;
}
// 引发编译错误
// if(new_enum::value1 == 1) {
// compile error
// if(new_enum::left_value1 == 1) {
// std::cout << "true!" << std::endl;
// }
if (new_enum::value3 == new_enum::value4) {