课程代码仓库初始化:Part1 (144) + Part3 (29) 全量生成与验证
把川大 wiki「02.Qt方向」课程代码整理为 CMake 管理的可编译运行项目。
Part1 C/C++ 强化 (p01/, 144 目标, host gcc/g++ 构建, 全绿):
- 修复全部编译失败:补 <cstring>/<fstream>/<iomanip>/<vector> 等缺失头
(common_fix + infer_stl_includes 自动检测注入)、修正 pause() 壳注入
(改检测已转换后的 pause 调用)、namespace 提升出函数体、const 成员函数
正确性、dedupe 不再破坏函数重载集、K&R 隐式 int / 动态异常规范等
C++17 移除特性的可编译等价改写。
- 目录采用深缩写 p01/ch04/s03/ (原 part1_cpp/ch04_class_object/03_ctor_dtor)。
Part3 Qt 桌面 (p03/, 29 目标, 隔离 Qt 5.14.2 构建, 全绿):
- gen_part3.py: Qt 外壳模板 (main+QApplication+show, QTimer 自动退出便于
offscreen 验证)、AUTOMOC + .moc include、rewrite_* 处理散文混入/重载/
资源依赖、生成占位 png/gif + .qrc + rundata 样例数据。
- 修正 QLable 笔误、全角分号、.→->、Windows 路径、QApplication 阻塞事件
循环改为 QCoreApplication 等。
任务1 概念/版本验证:
- tools/std_probe.py: 跨 -std= 编译探针 (--pedantic-errors 让已废除特性硬失败)。
- tools/demo_versions/: K&R 隐式 int、三目左值、void* 转换、enum 赋整、
const 经指针、动态异常规范 共 6 个跨版本演示。
- docs/VERSION_NOTES.md: 每条「原文说法→C17/C++17 是否成立→何版本成立→已验证」。
文档: 顶层 README、docs/SOURCE_PAGES.md (页面→目录映射)、
docs/ERRATA.md (Part1 修正)、docs/ERRATA_part3.md (Part3 修正)。
另修复 tools/run.sh / run_qt.sh 的 ${1:?...} 引号语法 bug。
隔离 Qt 验证: ldd 0 系统 Qt 引用; 173/173 目标全绿; p03 offscreen 运行通过。
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
# Auto-generated by tools/gen_part1.py — do not edit by hand.
|
||||
# 章节:p01/ch04/s03
|
||||
add_executable(p1c04_03_01 ctor_and_dtor.cpp)
|
||||
set_target_properties(p1c04_03_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_01 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_02 ctor_classification.cpp)
|
||||
set_target_properties(p1c04_03_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_02 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_03 copy_ctor_timing.cpp)
|
||||
set_target_properties(p1c04_03_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_03 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_04 deep_vs_shallow_copy.cpp)
|
||||
set_target_properties(p1c04_03_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_04 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_05 initializer_list.cpp)
|
||||
set_target_properties(p1c04_03_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_05 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_06 class_object_member.cpp)
|
||||
set_target_properties(p1c04_03_06 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_06 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_06 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_07 explicit_keyword.cpp)
|
||||
set_target_properties(p1c04_03_07 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_07 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_07 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_08 c_dynamic_alloc.cpp)
|
||||
set_target_properties(p1c04_03_08 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_08 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_08 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_09 new_operator.cpp)
|
||||
set_target_properties(p1c04_03_09 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_09 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_09 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_10 delete_operator.cpp)
|
||||
set_target_properties(p1c04_03_10 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_10 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_10 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_11 new_delete_array.cpp)
|
||||
set_target_properties(p1c04_03_11 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_11 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_11 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_12 new_delete_array_2.cpp)
|
||||
set_target_properties(p1c04_03_12 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_12 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_12 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_13 delete_void_ptr.cpp)
|
||||
set_target_properties(p1c04_03_13 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_13 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_13 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_14 static_member_variable.cpp)
|
||||
set_target_properties(p1c04_03_14 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_14 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_14 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_15 static_member_function.cpp)
|
||||
set_target_properties(p1c04_03_15 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_15 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_15 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_16 const_static_member.cpp)
|
||||
set_target_properties(p1c04_03_16 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_16 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_16 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_03_17 static_singleton.cpp)
|
||||
set_target_properties(p1c04_03_17 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_03_17 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_03_17 PRIVATE -Wall -Wextra)
|
||||
add_custom_target(all_p01_ch04_s03 DEPENDS p1c04_03_01 p1c04_03_02 p1c04_03_03 p1c04_03_04 p1c04_03_05 p1c04_03_06 p1c04_03_07 p1c04_03_08 p1c04_03_09 p1c04_03_10 p1c04_03_11 p1c04_03_12 p1c04_03_13 p1c04_03_14 p1c04_03_15 p1c04_03_16 p1c04_03_17)
|
||||
@@ -0,0 +1,52 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_08 — 4.3.9.2 C动态分配内存方法
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954451:7] 4.3.9.2 C动态分配内存方法 ---
|
||||
class Person{
|
||||
public:
|
||||
Person(){
|
||||
mAge = 20;
|
||||
pName = (char*)malloc(strlen("john")+1);
|
||||
strcpy(pName, "john");
|
||||
}
|
||||
void Init(){
|
||||
mAge = 20;
|
||||
pName = (char*)malloc(strlen("john")+1);
|
||||
strcpy(pName, "john");
|
||||
}
|
||||
void Clean(){
|
||||
if (pName != NULL){
|
||||
free(pName);
|
||||
}
|
||||
}
|
||||
public:
|
||||
int mAge;
|
||||
char* pName;
|
||||
};
|
||||
int main(){
|
||||
|
||||
//分配内存
|
||||
Person* person = (Person*)malloc(sizeof(Person));
|
||||
if(person == NULL){
|
||||
return 0;
|
||||
}
|
||||
//调用初始化函数
|
||||
person->Init();
|
||||
//清理对象
|
||||
person->Clean();
|
||||
//释放person对象
|
||||
free(person);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
@@ -0,0 +1,90 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_06 — 4.3.7.2 类对象作为成员
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954451:5] 4.3.7.2 类对象作为成员 ---
|
||||
//汽车类
|
||||
class Car{
|
||||
public:
|
||||
Car(){
|
||||
cout << "Car 默认构造函数!" << endl;
|
||||
mName = "大众汽车";
|
||||
}
|
||||
Car(string name){
|
||||
cout << "Car 带参数构造函数!" << endl;
|
||||
mName = name;
|
||||
}
|
||||
~Car(){
|
||||
cout << "Car 析构函数!" << endl;
|
||||
}
|
||||
public:
|
||||
string mName;
|
||||
};
|
||||
|
||||
//拖拉机
|
||||
class Tractor{
|
||||
public:
|
||||
Tractor(){
|
||||
cout << "Tractor 默认构造函数!" << endl;
|
||||
mName = "爬土坡专用拖拉机";
|
||||
}
|
||||
Tractor(string name){
|
||||
cout << "Tractor 带参数构造函数!" << endl;
|
||||
mName = name;
|
||||
}
|
||||
~Tractor(){
|
||||
cout << "Tractor 析构函数!" << endl;
|
||||
}
|
||||
public:
|
||||
string mName;
|
||||
};
|
||||
|
||||
//人类
|
||||
class Person{
|
||||
public:
|
||||
#if 1
|
||||
//类mCar不存在合适的构造函数
|
||||
Person(string name){
|
||||
mName = name;
|
||||
}
|
||||
#else
|
||||
//初始化列表可以指定调用构造函数
|
||||
Person(string carName, string tracName, string name) : mTractor(tracName), mCar(carName), mName(name){
|
||||
cout << "Person 构造函数!" << endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
void GoWorkByCar(){
|
||||
cout << mName << "开着" << mCar.mName << "去上班!" << endl;
|
||||
}
|
||||
void GoWorkByTractor(){
|
||||
cout << mName << "开着" << mTractor.mName << "去上班!" << endl;
|
||||
}
|
||||
~Person(){
|
||||
cout << "Person 析构函数!" << endl;
|
||||
}
|
||||
private:
|
||||
string mName;
|
||||
Car mCar;
|
||||
Tractor mTractor;
|
||||
};
|
||||
|
||||
void test(){
|
||||
//Person person("宝马", "东风拖拉机", "赵四");
|
||||
Person person("刘能");
|
||||
person.GoWorkByCar();
|
||||
person.GoWorkByTractor();
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_16 — 4.3.10.3 const静态成员属性
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954451:15] 4.3.10.3 const静态成员属性 ---
|
||||
class Person{
|
||||
public:
|
||||
//static const int mShare = 10;
|
||||
const static int mShare = 10; //只读区,不可修改
|
||||
};
|
||||
int main(){
|
||||
|
||||
cout << Person::mShare << endl;
|
||||
//Person::mShare = 20;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
@@ -0,0 +1,69 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_03 — 4.3.4 拷贝构造函数的调用时机
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <algorithm>
|
||||
|
||||
// --- 原文片段 [58954451:2] 4.3.4 拷贝构造函数的调用时机 ---
|
||||
class Person{
|
||||
public:
|
||||
Person(){
|
||||
cout << "no param contructor!" << endl;
|
||||
mAge = 10;
|
||||
}
|
||||
Person(int age){
|
||||
cout << "param constructor!" << endl;
|
||||
mAge = age;
|
||||
}
|
||||
Person(const Person& person){
|
||||
cout << "copy constructor!" << endl;
|
||||
mAge = person.mAge;
|
||||
}
|
||||
~Person(){
|
||||
cout << "destructor!" << endl;
|
||||
}
|
||||
public:
|
||||
int mAge;
|
||||
};
|
||||
//1. 旧对象初始化新对象
|
||||
void test01(){
|
||||
|
||||
Person p(10);
|
||||
Person p1(p);
|
||||
Person p2 = Person(p);
|
||||
Person p3 = p; // 相当于Person p2 = Person(p);
|
||||
}
|
||||
|
||||
//2. 传递的参数是普通对象,函数参数也是普通对象,传递将会调用拷贝构造
|
||||
void doBussiness(Person p){}
|
||||
|
||||
void test02(){
|
||||
Person p(10);
|
||||
doBussiness(p);
|
||||
}
|
||||
|
||||
//3. 函数返回局部对象
|
||||
Person MyBusiness(){
|
||||
Person p(10);
|
||||
cout << "局部p:" << (int*)&p << endl;
|
||||
return p;
|
||||
}
|
||||
void test03(){
|
||||
//vs release、qt下没有调用拷贝构造函数
|
||||
//vs debug下调用一次拷贝构造函数
|
||||
Person p = MyBusiness();
|
||||
cout << "局部p:" << (int*)&p << endl;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test01();
|
||||
test03();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_01 — 4.3.2 构造函数和析构函数
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954451:0] 4.3.2 构造函数和析构函数 ---
|
||||
class Person{
|
||||
public:
|
||||
Person(){
|
||||
cout << "构造函数调用!" << endl;
|
||||
pName = (char*)malloc(sizeof("John"));
|
||||
strcpy(pName, "John");
|
||||
mTall = 150;
|
||||
mMoney = 100;
|
||||
}
|
||||
~Person(){
|
||||
cout << "析构函数调用!" << endl;
|
||||
if (pName != NULL){
|
||||
free(pName);
|
||||
pName = NULL;
|
||||
}
|
||||
}
|
||||
public:
|
||||
char* pName;
|
||||
int mTall;
|
||||
int mMoney;
|
||||
};
|
||||
|
||||
void test(){
|
||||
Person person;
|
||||
cout << person.pName << person.mTall << person.mMoney << endl;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_02 — 4.3.3 构造函数的分类及调用
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <algorithm>
|
||||
|
||||
// --- 原文片段 [58954451:1] 4.3.3 构造函数的分类及调用 ---
|
||||
class Person{
|
||||
public:
|
||||
Person(){
|
||||
cout << "no param constructor!" << endl;
|
||||
mAge = 0;
|
||||
}
|
||||
//有参构造函数
|
||||
Person(int age){
|
||||
cout << "1 param constructor!" << endl;
|
||||
mAge = age;
|
||||
}
|
||||
//拷贝构造函数(复制构造函数) 使用另一个对象初始化本对象
|
||||
Person(const Person& person){
|
||||
cout << "copy constructor!" << endl;
|
||||
mAge = person.mAge;
|
||||
}
|
||||
//打印年龄
|
||||
void PrintPerson(){
|
||||
cout << "Age:" << mAge << endl;
|
||||
}
|
||||
private:
|
||||
int mAge;
|
||||
};
|
||||
//1. 无参构造调用方式
|
||||
void test01(){
|
||||
|
||||
//调用无参构造函数
|
||||
Person person1;
|
||||
person1.PrintPerson();
|
||||
|
||||
//无参构造函数错误调用方式
|
||||
//Person person2();
|
||||
//person2.PrintPerson();
|
||||
}
|
||||
//2. 调用有参构造函数
|
||||
void test02(){
|
||||
|
||||
//第一种 括号法,最常用
|
||||
Person person01(100);
|
||||
person01.PrintPerson();
|
||||
|
||||
//调用拷贝构造函数
|
||||
Person person02(person01);
|
||||
person02.PrintPerson();
|
||||
|
||||
//第二种 匿名对象(显示调用构造函数)
|
||||
Person(200); //匿名对象,没有名字的对象
|
||||
|
||||
Person person03 = Person(300);
|
||||
person03.PrintPerson();
|
||||
|
||||
//注意: 使用匿名对象初始化判断调用哪一个构造函数,要看匿名对象的参数类型
|
||||
Person person06(Person(400)); //等价于 Person person06 = Person(400);
|
||||
person06.PrintPerson();
|
||||
|
||||
//第三种 =号法 隐式转换
|
||||
Person person04 = 100; //Person person04 = Person(100)
|
||||
person04.PrintPerson();
|
||||
|
||||
//调用拷贝构造
|
||||
Person person05 = person04; //Person person05 = Person(person04)
|
||||
person05.PrintPerson();
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test01();
|
||||
test02();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_04 — 4.3.6 深拷贝和浅拷贝
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954451:3] 4.3.6 深拷贝和浅拷贝 ---
|
||||
class Person{
|
||||
public:
|
||||
Person(char* name,int age){
|
||||
pName = (char*)malloc(strlen(name) + 1);
|
||||
strcpy(pName,name);
|
||||
mAge = age;
|
||||
}
|
||||
//增加拷贝构造函数
|
||||
Person(const Person& person){
|
||||
pName = (char*)malloc(strlen(person.pName) + 1);
|
||||
strcpy(pName, person.pName);
|
||||
mAge = person.mAge;
|
||||
}
|
||||
~Person(){
|
||||
if (pName != NULL){
|
||||
free(pName);
|
||||
}
|
||||
}
|
||||
private:
|
||||
char* pName;
|
||||
int mAge;
|
||||
};
|
||||
|
||||
void test(){
|
||||
Person p1("Edward",30);
|
||||
//用对象p1初始化对象p2,调用c++提供的默认拷贝构造函数
|
||||
Person p2 = p1;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_10 — 4.3.9.4 delete operator
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954451:9] 4.3.9.4 delete operator ---
|
||||
class Person{
|
||||
public:
|
||||
Person(){
|
||||
cout << "无参构造函数!" << endl;
|
||||
pName = (char*)malloc(strlen("undefined") + 1);
|
||||
strcpy(pName, "undefined");
|
||||
mAge = 0;
|
||||
}
|
||||
Person(char* name, int age){
|
||||
cout << "有参构造函数!" << endl;
|
||||
pName = (char*)malloc(strlen(name) + 1);
|
||||
strcpy(pName, name);
|
||||
mAge = age;
|
||||
}
|
||||
void ShowPerson(){
|
||||
cout << "Name:" << pName << " Age:" << mAge << endl;
|
||||
}
|
||||
~Person(){
|
||||
cout << "析构函数!" << endl;
|
||||
if (pName != NULL){
|
||||
delete pName;
|
||||
pName = NULL;
|
||||
}
|
||||
}
|
||||
public:
|
||||
char* pName;
|
||||
int mAge;
|
||||
};
|
||||
|
||||
void test(){
|
||||
Person* person1 = new Person;
|
||||
Person* person2 = new Person("John",33);
|
||||
|
||||
person1->ShowPerson();
|
||||
person2->ShowPerson();
|
||||
|
||||
delete person1;
|
||||
delete person2;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_13 — 4.3.9.6 delete void*可能会出错
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954451:12] 4.3.9.6 delete void*可能会出错 ---
|
||||
class Person{
|
||||
public:
|
||||
Person(char* name, int age){
|
||||
pName = (char*)malloc(sizeof(name));
|
||||
strcpy(pName,name);
|
||||
mAge = age;
|
||||
}
|
||||
~Person(){
|
||||
if (pName != NULL){
|
||||
delete pName;
|
||||
}
|
||||
}
|
||||
public:
|
||||
char* pName;
|
||||
int mAge;
|
||||
};
|
||||
|
||||
void test(){
|
||||
void* person = new Person("john",20);
|
||||
delete person;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_07 — 4.3.8 explicit关键字
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954451:6] 4.3.8 explicit关键字 ---
|
||||
class MyString{
|
||||
public:
|
||||
explicit MyString(int n){
|
||||
cout << "MyString(int n)!" << endl;
|
||||
}
|
||||
MyString(const char* str){
|
||||
cout << "MyString(const char* str)" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
//给字符串赋值?还是初始化?
|
||||
//MyString str1 = 1;
|
||||
MyString str2(10);
|
||||
|
||||
//寓意非常明确,给字符串赋值
|
||||
MyString str3 = "abcd";
|
||||
MyString str4("abcd");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
@@ -0,0 +1,42 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_05 — 4.3.7.1 初始化列表
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954451:4] 4.3.7.1 初始化列表 ---
|
||||
static void block_0() {
|
||||
class Person{
|
||||
public:
|
||||
#if 0
|
||||
//传统方式初始化
|
||||
Person(int a,int b,int c){
|
||||
mA = a;
|
||||
mB = b;
|
||||
mC = c;
|
||||
}
|
||||
#endif
|
||||
//初始化列表方式初始化
|
||||
Person(int a, int b, int c):mA(a),mB(b),mC(c){}
|
||||
void PrintPerson(){
|
||||
cout << "mA:" << mA << endl;
|
||||
cout << "mB:" << mB << endl;
|
||||
cout << "mC:" << mC << endl;
|
||||
}
|
||||
private:
|
||||
int mA;
|
||||
int mB;
|
||||
int mC;
|
||||
};
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
block_0();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_11 — 4.3.9.5 用于数组的new和delete
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954451:10] 4.3.9.5 用于数组的new和delete ---
|
||||
static void block_0() {
|
||||
//创建字符数组
|
||||
char* pStr = new char[100];
|
||||
//创建整型数组
|
||||
int* pArr1 = new int[100];
|
||||
//创建整型数组并初始化
|
||||
int* pArr2 = new int[10]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
|
||||
//释放数组内存
|
||||
delete[] pStr;
|
||||
delete[] pArr1;
|
||||
delete[] pArr2;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
block_0();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_12 — 4.3.9.5 用于数组的new和delete
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954451:11] 4.3.9.5 用于数组的new和delete ---
|
||||
class Person{
|
||||
public:
|
||||
Person(){
|
||||
pName = (char*)malloc(strlen("undefined") + 1);
|
||||
strcpy(pName, "undefined");
|
||||
mAge = 0;
|
||||
}
|
||||
Person(char* name, int age){
|
||||
pName = (char*)malloc(sizeof(name));
|
||||
strcpy(pName, name);
|
||||
mAge = age;
|
||||
}
|
||||
~Person(){
|
||||
if (pName != NULL){
|
||||
delete pName;
|
||||
}
|
||||
}
|
||||
public:
|
||||
char* pName;
|
||||
int mAge;
|
||||
};
|
||||
|
||||
void test(){
|
||||
//栈聚合初始化
|
||||
Person person[] = { Person("john", 20), Person("Smith", 22) };
|
||||
cout << person[1].pName << endl;
|
||||
//创建堆上对象数组必须提供构造函数
|
||||
Person* workers = new Person[20];
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_09 — 4.3.9.3 new operator
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954451:8] 4.3.9.3 new operator ---
|
||||
// 原文为伪代码对照,演示 new 与 malloc 的等价关系:
|
||||
// Person* person = new Person;
|
||||
// 相当于:
|
||||
// Person* person = (Person*)malloc(sizeof(Person));
|
||||
// if(person == NULL){ return 0; }
|
||||
// person->Init(); // 构造函数
|
||||
// 下面给出可编译的对照程序:
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
class Person {
|
||||
public:
|
||||
Person() { std::cout << "构造函数!" << std::endl; }
|
||||
void Init() { std::cout << "Init" << std::endl; }
|
||||
};
|
||||
int main() {
|
||||
Person* person = new Person; // C++: new 自动调用构造函数
|
||||
Person* p2 = (Person*)malloc(sizeof(Person));
|
||||
if (p2) { new (p2) Person(); } // 对齐 new 的行为(placement new)
|
||||
delete person;
|
||||
if (p2) { p2->~Person(); free(p2); }
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
@@ -0,0 +1,54 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_15 — 4.3.10.2 静态成员函数
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954451:14] 4.3.10.2 静态成员函数 ---
|
||||
class Person{
|
||||
public:
|
||||
//普通成员函数可以访问static和non-static成员属性
|
||||
void changeParam1(int param){
|
||||
mParam = param;
|
||||
sNum = param;
|
||||
}
|
||||
//静态成员函数只能访问static成员属性
|
||||
static void changeParam2(int param){
|
||||
//mParam = param; //无法访问
|
||||
sNum = param;
|
||||
}
|
||||
private:
|
||||
static void changeParam3(int param){
|
||||
//mParam = param; //无法访问
|
||||
sNum = param;
|
||||
}
|
||||
public:
|
||||
int mParam;
|
||||
static int sNum;
|
||||
};
|
||||
|
||||
//静态成员属性类外初始化
|
||||
int Person::sNum = 0;
|
||||
|
||||
int main(){
|
||||
|
||||
//1. 类名直接调用
|
||||
Person::changeParam2(100);
|
||||
|
||||
//2. 通过对象调用
|
||||
Person p;
|
||||
p.changeParam2(200);
|
||||
|
||||
//3. 静态成员函数也有访问权限
|
||||
//Person::changeParam3(100); //类外无法访问私有静态成员函数
|
||||
//Person p1;
|
||||
//p1.changeParam3(200);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
@@ -0,0 +1,50 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_14 — 4.3.10.1 静态成员变量
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include <cstdio>
|
||||
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
|
||||
|
||||
// --- 原文片段 [58954451:13] 4.3.10.1 静态成员变量 ---
|
||||
class Person{
|
||||
public:
|
||||
//类的静态成员属性
|
||||
static int sNum;
|
||||
private:
|
||||
static int sOther;
|
||||
};
|
||||
|
||||
//类外初始化,初始化时不加static
|
||||
int Person::sNum = 0;
|
||||
int Person::sOther = 0;
|
||||
int main(){
|
||||
|
||||
|
||||
//1. 通过类名直接访问
|
||||
Person::sNum = 100;
|
||||
cout << "Person::sNum:" << Person::sNum << endl;
|
||||
|
||||
//2. 通过对象访问
|
||||
Person p1, p2;
|
||||
p1.sNum = 200;
|
||||
|
||||
cout << "p1.sNum:" << p1.sNum << endl;
|
||||
cout << "p2.sNum:" << p2.sNum << endl;
|
||||
|
||||
//3. 静态成员也有访问权限,类外不能访问私有成员
|
||||
//cout << "Person::sOther:" << Person::sOther << endl;
|
||||
Person p3;
|
||||
//cout << "p3.sOther:" << p3.sOther << endl;
|
||||
|
||||
pause();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
@@ -0,0 +1,43 @@
|
||||
// ============================================================================
|
||||
// p1c04_03_17 — 4.3.10.4 静态成员实现单例模式
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954451:16] 4.3.10.4 静态成员实现单例模式 ---
|
||||
class Printer{
|
||||
public:
|
||||
static Printer* getInstance(){ return pPrinter;}
|
||||
void PrintText(string text){
|
||||
cout << "打印内容:" << text << endl;
|
||||
cout << "已打印次数:" << mTimes << endl;
|
||||
cout << "--------------" << endl;
|
||||
mTimes++;
|
||||
}
|
||||
private:
|
||||
Printer(){ mTimes = 0; }
|
||||
Printer(const Printer&){}
|
||||
private:
|
||||
static Printer* pPrinter;
|
||||
int mTimes;
|
||||
};
|
||||
|
||||
Printer* Printer::pPrinter = new Printer;
|
||||
|
||||
void test(){
|
||||
Printer* printer = Printer::getInstance();
|
||||
printer->PrintText("离职报告!");
|
||||
printer->PrintText("入职合同!");
|
||||
printer->PrintText("提交代码!");
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user