This commit is contained in:
Light-City 2019-12-25 21:06:30 +08:00
parent 8544284f51
commit 095ebc9011
8 changed files with 148 additions and 29 deletions

View File

@ -142,6 +142,7 @@ for(decl:col) {
- [容器1](./modern_C++_30/container1)
- [容器2](./modern_C++_30/container2)
- [异常](./modern_C++_30/exception)
- [字面量、静态断言和成员函数说明符](./modern_C++_30/literalAssert)
- [是不是应该返回对象?](./modern_C++_30/returnObj)
### 4.拓展部分

Binary file not shown.

View File

@ -35,3 +35,8 @@ add_executable(returnObj3 returnObj/returnObj3.cpp)
add_executable(returnObj4 returnObj/returnObj4.cpp)
add_executable(returnObj5 returnObj/returnObj5.cpp)
add_executable(rvonrvo returnObj/all.cpp)
add_executable(literal literalAssert/literal.cpp)
add_executable(assert literalAssert/assert.cpp)
add_executable(default_delete literalAssert/default_delete.cpp)
add_executable(overridefinal literalAssert/overridefinal.cpp)

View File

@ -1,29 +0,0 @@
cmake_minimum_required(VERSION 3.14)
project(Morden_C++)
set(CMAKE_CXX_STANDARD 11)
add_executable(heap RAII/heap.cpp)
add_executable(stack RAII/stack.cpp)
add_executable(RAII RAII/RAII.cpp)
add_executable(RAII_fstram RAII/RAII_fstram.cpp)
add_executable(auto_scope smart_ptr/auto_scope.cpp)
add_executable(unique_ptr smart_ptr/unique_ptr.cpp)
add_executable(unique_ptr_U smart_ptr/unique_ptr_U.cpp)
add_executable(shared_ptr smart_ptr/shared_ptr.cpp)
add_executable(reference reference/reference.cpp)
add_executable(forward reference/forward.cpp)
add_executable(collapses reference/collapses.cpp)
add_executable(lifetime reference/lifetime.cpp)
add_executable(dontreturnReference reference/don'treturnReference.cpp)
add_executable(container container1/container.cpp)
add_executable(cont container2/hash.cpp)
add_executable(vector_l container1/vector_l.cpp)
add_executable(priority_queue container2/priority_queue.cpp)
add_executable(relacontainer container2/relacontainer.cpp)
add_executable(unorder container2/unorder.cpp)
add_executable(array container2/array.cpp)
add_executable(exception exception/exception.cpp)

View File

@ -0,0 +1,13 @@
//
// Created by light on 19-12-25.
//
#include <iostream>
#include <cassert>
int main() {
const int alignment=5;
assert((alignment & (alignment - 1)) == 0);
static_assert((alignment & (alignment - 1)) == 0, "Alignment must be power of two");
return 0;
}

View File

@ -0,0 +1,18 @@
//
// Created by light on 19-12-25.
//
class myFun {
public:
myFun() = default;
myFun(const myFun &) = default;
myFun &operator=(const myFun &) = default;
myFun(myFun &&) = delete;
myFun &operator=(myFun &&) = delete;
~myFun() = default;
};

View File

@ -0,0 +1,81 @@
//
// Created by light on 19-12-25.
//
#include <chrono>
#include <complex>
#include <iostream>
#include <string>
#include <thread>
#include <bitset>
using namespace std::literals::chrono_literals;
using namespace std::literals::string_literals;
using namespace std::literals::complex_literals;
struct length {
double value;
enum unit {
metre,
kilometre,
millimetre,
centimetre,
inch,
foot,
yard,
mile,
};
static constexpr double factors[] =
{1.0, 1000.0, 1e-3,
1e-2, 0.0254, 0.3048,
0.9144, 1609.344};
explicit length(double v,
unit u = metre) {
value = v * factors[u];
}
};
length operator+(length lhs,
length rhs) {
return length(lhs.value +
rhs.value);
}
length operator "" _m(long double v) { return length(v, length::metre); }
length operator "" _cm(long double v) { return length(v, length::centimetre); }
// 可能有其他运算符
int main() {
std::cout << "i * i = " << 1i * 1i << std::endl;
std::cout << "Waiting for 500ms" << std::endl;
std::this_thread::sleep_for(500ms);
std::cout << "Hello world"s.substr(0, 5) << std::endl;
length l1 = length(1.0, length::metre);
length l2 = length(1.0, length::centimetre);
std::cout << l2.value << std::endl;
std::cout << (l1 + l2).value << std::endl;
// 1.0_m + 10.0_cm
std::cout << (1.0_m + 1.0_cm).value << std::endl;
// 二进制字面量
unsigned mask = 0b1101;
// 以十进制打印
std::cout << mask << std::endl;
// 打印二进制字面量
std::cout << std::bitset<4>(mask) << std::endl;
// 数字分隔符
unsigned mk = 0b111'000'000;
double pi = 3.141'5926;
return 0;
}

View File

@ -0,0 +1,30 @@
//
// Created by light on 19-12-25.
//
class A {
public:
virtual void foo();
virtual void bar();
void foobar();
};
class B : public A {
public:
void foo() override; // OK
void bar() override final; // OK
//void foobar() override;
// 非虚函数不能 override
};
class C final : public B {
public:
void foo() override; // OK
//void bar() override;
// final 函数不可 override
};
class D : public C {
// 错误final 类不可派生
};