版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42528266/article/details/102759289
#include <iostream>
using namespace std;
class Person {
public:
int m_age;
};
void testAuto() {
auto i = 10;
auto name = "jack";
auto d = 9.6;
auto p = new Person();
p->m_age = 10;
}
void func(int p) {
cout << "func(int) - " << p << endl;
}
void func(int *p) {
cout << "func(int *) - " << p << endl;
}
void test_nullptr() {
/*int i = 10;
const char *name = "jack";
double d = 9.6;*/
// int i = 0;
// int *p = nullptr;
/*func(0);
func(NULL);
func(nullptr);
cout << (nullptr == NULL) << endl;*/
int array[]{ 10, 20, 30, 40 };
for (int item : array) {
cout << item << endl;
}
/*int length = sizeof(array) / sizeof(int);
for (int i = 0; i < length; i++)
{
cout << array[i] << endl;
}*/
}
//int sum(int a, int b) { return a + b; }
//int minus1(int a, int b) { return a - b; }
//int multiple(int a, int b) { return a * b; }
//int divide(int a, int b) { return a / b; }
int exec(int a, int b, int (*func)(int, int)) {
// ....
// ...
return func(a, b);
}
int main() {
/*int (*p)(int, int) = [](int a, int b) -> int {
return a + b;
};
cout << p(10, 20) << endl;*/
/*cout << [](int a, int b) -> int {
return a + b;
}(10, 20) << endl;*/
/*cout << [](int a, int b) {
return a + b;
}(10, 20) << endl;*/
/*void(*p)() = [] {
cout << 1 << endl;
cout << 2 << endl;
cout << 3 << endl;
};
p();
p();*/
/*cout << exec(20, 10, sum) << endl;
cout << exec(20, 10, minus1) << endl;
cout << exec(20, 10, multiple) << endl;
cout << exec(20, 10, divide) << endl;*/
/*cout << exec(20, 10, [](int a, int b) { return a + b; }) << endl;
cout << exec(20, 10, [](int a, int b) { return a - b; }) << endl;
cout << exec(20, 10, [](int a, int b) { return a * b; }) << endl;
cout << exec(20, 10, [](int a, int b) { return a / b; }) << endl;*/
//int a = 10;
//int b = 20;
//auto p = [a] () mutable {
// a++;
// cout << "lambda - " << a << endl;
//};
//p();
//// 10
//cout << a << endl;
// 默认情况是值捕获
/*auto p = [=, &a] {
cout << a << endl;
cout << b << endl;
};
a = 11;
b = 22;
p();*/
if (int a = 10; a > 5) {
}
else if (int b = 20; b > 10) {
}
else if (2) {
}
else {
}
getchar();
return 0;
}
#include <iostream>
using namespace std;
//int sum(int a, int b) {
// // 业务逻辑写错了
// return a - b;
//}
void test() {
cout << "1" << endl;
// 如果内存不够,就会抛出异常(运行过程中抛出一个错误)
try {
for (int i = 0; i < 9999999; i++) {
int *p = new int[9999999];
}
cout << "2" << endl;
}
catch (...) {
cout << "发生了异常" << endl;
}
cout << "3" << endl;
// delete[] p;
}
void test(int a, int b, int c) {
if (a <= 0) return;
}
int divide(int a, int b) {
if (b == 0) throw "不能除以0";
return a / b;
}
void test2() {
cout << "1" << endl;
try {
int v1 = 10;
int v2 = 0;
cout << divide(v1, v2) << endl;
}
catch (int exception) {
cout << "捕捉到异常1:" << exception << endl;
}
catch (const char *exception) {
cout << "捕捉到异常2:" << exception << endl;
}
cout << "2" << endl;
}
void test3() {
try {
throw 3;
} catch (const char *exception) {
cout << "test3-捕捉到异常:" << exception << endl;
}
// 432432
// 4324234
// .....
}
void test4() {
try {
test3();
} catch (int exception) {
cout << "test4-捕捉到异常:" << exception << endl;
}
// .....
}
class Exception {
public:
virtual const char *what() const = 0;
};
class DivideException : public Exception {
public:
const char *what() const {
return "不能除以0";
}
};
class AddException : public Exception {
public:
const char *what() const {
return "加法有问题";
}
};
int divide2(int a, int b) {
if (b == 0) throw DivideException();
return a / b;
}
int main() {
/*try {
divide2(10, 0);
} catch (const DivideException &e) {
cout << e.what() << endl;
} catch (const AddException &e) {
}*/
try {
for (int i = 0; i < 9999999; i++) {
int *p = new int[9999999];
}
// 234234234
} catch (bad_alloc e) {
cout << e.what() << endl;
}
getchar();
return 0;
}
#include <iostream>
using namespace std;
void test() {
throw 6;
}
void test1() {
try {
int *p = new int();
test();
delete p;
}
catch (...) {
cout << "出现异常" << endl;
}
}
class Person {
public:
Person() {
cout << "Person()" << endl;
}
~Person() {
cout << "~Person()" << endl;
}
void run() {
cout << "Person::run()" << endl;
}
};
void test2() {
//cout << "1" << endl;
//
//{
// // Person *p = new Person();
// auto_ptr<Person> p(new Person());
// p->run();
//}
//cout << "2" << endl;
}
template <class T>
class SmartPointer {
T *m_pointer;
public:
SmartPointer(T *pointer) :m_pointer(pointer) { }
~SmartPointer() {
if (m_pointer == nullptr) return;
delete m_pointer;
}
T *operator->() {
return m_pointer;
}
};
int main() {
cout << "1" << endl;
{
SmartPointer<Person> p(new Person());
p->run();
}
cout << "2" << endl;
getchar();
return 0;
}
#include <iostream>
using namespace std;
class Person;
class Car {
public:
// Person *m_person;
// shared_ptr<Person> m_person = nullptr;
weak_ptr<Person> m_person;
Car() {
cout << "Car()" << endl;
}
~Car() {
cout << "~Car()" << endl;
}
};
class Person {
public:
// Car *m_car;
shared_ptr<Car> m_car = nullptr;
Person() {
cout << "Person()" << endl;
}
~Person() {
cout << "~Person()" << endl;
}
void run() {
cout << "Person::run()" << endl;
}
};
void test1() {
cout << "1" << endl;
{
shared_ptr<Person> p2;
{
shared_ptr<Person> p1(new Person());
p2 = p1;
cout << "2" << endl;
}
cout << "3" << endl;
}
cout << "4" << endl;
}
void test2() {
/*auto expr = [](Person *p) {
delete[] p;
};
shared_ptr<Person> p1(new Person[5]{}, expr);*/
shared_ptr<Person[]> p2(new Person[5]{});
}
void test3() {
{
shared_ptr<Person> p1(new Person());
cout << p1.use_count() << endl; // 1
{
shared_ptr<Person> p2 = p1;
cout << p1.use_count() << endl; // 2
}
cout << p1.use_count() << endl; // 1
{
shared_ptr<Person> p3 = p1;
cout << p1.use_count() << endl; // 2
}
cout << p1.use_count() << endl; // 1
shared_ptr<Person> p4 = p1;
cout << p1.use_count() << endl; // 2
} // 0
}
void test4() {
Person *p = new Person();
{
shared_ptr<Person> p1(p);
}
{
shared_ptr<Person> p2(p);
}
}
void test5() {
/*Person *p = new Person();
p->m_car = new Car();
p->m_car->m_person = p;*/
cout << "1" << endl;
{
shared_ptr<Person> person(new Person());
shared_ptr<Car> car(new Car());
person->m_car = car;
car->m_person = person;
}
cout << "2" << endl;
}
int main() {
{
unique_ptr<Person> p1(new Person());
{
unique_ptr<Person> p2 = std::move(p1);
cout << "1" << endl;
}
cout << "2" << endl;
}
getchar();
return 0;
}