课程代码仓库初始化: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,27 @@
|
||||
# Auto-generated by tools/gen_part1.py — do not edit by hand.
|
||||
# 章节:p01/ch04/s06
|
||||
add_executable(p1c04_06_01 overload_with_friend.cpp)
|
||||
set_target_properties(p1c04_06_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_06_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_06_01 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_06_02 overload_increment_decrement.cpp)
|
||||
set_target_properties(p1c04_06_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_06_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_06_02 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_06_03 overload_pointer_operator.cpp)
|
||||
set_target_properties(p1c04_06_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_06_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_06_03 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_06_04 overload_assignment.cpp)
|
||||
set_target_properties(p1c04_06_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_06_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_06_04 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_06_05 overload_equality.cpp)
|
||||
set_target_properties(p1c04_06_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_06_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_06_05 PRIVATE -Wall -Wextra)
|
||||
add_executable(p1c04_06_06 overload_function_call.cpp)
|
||||
set_target_properties(p1c04_06_06 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
|
||||
set_target_properties(p1c04_06_06 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
|
||||
target_compile_options(p1c04_06_06 PRIVATE -Wall -Wextra)
|
||||
add_custom_target(all_p01_ch04_s06 DEPENDS p1c04_06_01 p1c04_06_02 p1c04_06_03 p1c04_06_04 p1c04_06_05 p1c04_06_06)
|
||||
@@ -0,0 +1,114 @@
|
||||
// ============================================================================
|
||||
// p1c04_06_04 — 4.6.6 赋值(=)运算符重载
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954458:3] 4.6.6 赋值(=)运算符重载 ---
|
||||
class Person{
|
||||
friend ostream& operator<<(ostream& os,const Person& person){
|
||||
os << "ID:" << person.mID << " Age:" << person.mAge << endl;
|
||||
return os;
|
||||
}
|
||||
public:
|
||||
Person(int id,int age){
|
||||
this->mID = id;
|
||||
this->mAge = age;
|
||||
}
|
||||
//重载赋值运算符
|
||||
Person& operator=(const Person& person){
|
||||
this->mID = person.mID;
|
||||
this->mAge = person.mAge;
|
||||
return *this;
|
||||
}
|
||||
private:
|
||||
int mID;
|
||||
int mAge;
|
||||
};
|
||||
|
||||
//1. =号混淆的地方
|
||||
void test01(){
|
||||
Person person1(10, 20);
|
||||
Person person2 = person1; //调用拷贝构造
|
||||
//如果一个对象还没有被创建,则必须初始化,也就是调用构造函数
|
||||
//上述例子由于person2还没有初始化,所以会调用构造函数
|
||||
//由于person2是从已有的person1来创建的,所以只有一个选择
|
||||
//就是调用拷贝构造函数
|
||||
person2 = person1; //调用operator=函数
|
||||
//由于person2已经创建,不需要再调用构造函数,这时候调用的是重载的赋值运算符
|
||||
}
|
||||
//2. 赋值重载案例
|
||||
void test02(){
|
||||
Person person1(20, 20);
|
||||
Person person2(30, 30);
|
||||
cout << "person1:" << person1;
|
||||
cout << "person2:" << person2;
|
||||
person2 = person1;
|
||||
cout << "person2:" << person2;
|
||||
}
|
||||
//常见错误,当准备给两个相同对象赋值时,应该首先检查一下这个对象是否对自身赋值了
|
||||
//对于本例来讲,无论如何执行这些赋值运算都是无害的,但如果对类的实现进行修改,那么将会出现差异;
|
||||
//3. 类中指针
|
||||
class Person2{
|
||||
friend ostream& operator<<(ostream& os, const Person2& person){
|
||||
os << "Name:" << person.pName << " ID:" << person.mID << " Age:" << person.mAge << endl;
|
||||
return os;
|
||||
}
|
||||
public:
|
||||
Person2(char* name,int id, int age){
|
||||
this->pName = new char[strlen(name) + 1];
|
||||
strcpy(this->pName, name);
|
||||
this->mID = id;
|
||||
this->mAge = age;
|
||||
}
|
||||
#if 1
|
||||
//重载赋值运算符
|
||||
Person2& operator=(const Person2& person){
|
||||
|
||||
//注意:由于当前对象已经创建完毕,那么就有可能pName指向堆内存
|
||||
//这个时候如果直接赋值,会导致内存没有及时释放
|
||||
if (this->pName != NULL){
|
||||
delete[] this->pName;
|
||||
}
|
||||
|
||||
this->pName = new char[strlen(person.pName) + 1];
|
||||
strcpy(this->pName,person.pName);
|
||||
this->mID = person.mID;
|
||||
this->mAge = person.mAge;
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
//析构函数
|
||||
~Person2(){
|
||||
if (this->pName != NULL){
|
||||
delete[] this->pName;
|
||||
}
|
||||
}
|
||||
private:
|
||||
char* pName;
|
||||
int mID;
|
||||
int mAge;
|
||||
};
|
||||
|
||||
void test03(){
|
||||
Person2 person1("John",20, 20);
|
||||
Person2 person2("Edward",30, 30);
|
||||
cout << "person1:" << person1;
|
||||
cout << "person2:" << person2;
|
||||
person2 = person1;
|
||||
cout << "person2:" << person2;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test01();
|
||||
test02();
|
||||
test03();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// ============================================================================
|
||||
// p1c04_06_05 — 4.6.7 等于和不等于(==、!=)运算符重载
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include <cstring>
|
||||
|
||||
// --- 原文片段 [58954458:4] 4.6.7 等于和不等于(==、!=)运算符重载 ---
|
||||
class Complex{
|
||||
public:
|
||||
Complex(char* name,int id,int age){
|
||||
this->pName = new char[strlen(name) + 1];
|
||||
strcpy(this->pName, name);
|
||||
this->mID = id;
|
||||
this->mAge = age;
|
||||
}
|
||||
//重载==号操作符
|
||||
bool operator==(const Complex& complex){
|
||||
if (strcmp(this->pName,complex.pName) == 0 &&
|
||||
this->mID == complex.mID &&
|
||||
this->mAge == complex.mAge){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//重载!=操作符
|
||||
bool operator!=(const Complex& complex){
|
||||
if (strcmp(this->pName, complex.pName) != 0 ||
|
||||
this->mID != complex.mID ||
|
||||
this->mAge != complex.mAge){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
~Complex(){
|
||||
if (this->pName != NULL){
|
||||
delete[] this->pName;
|
||||
}
|
||||
}
|
||||
private:
|
||||
char* pName;
|
||||
int mID;
|
||||
int mAge;
|
||||
};
|
||||
void test(){
|
||||
Complex complex1("aaa", 10, 20);
|
||||
Complex complex2("bbb", 10, 20);
|
||||
if (complex1 == complex2){ cout << "相等!" << endl; }
|
||||
if (complex1 != complex2){ cout << "不相等!" << endl; }
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// ============================================================================
|
||||
// p1c04_06_06 — 4.6.8 函数调用符号()重载
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954458:5] 4.6.8 函数调用符号()重载 ---
|
||||
class Complex{
|
||||
public:
|
||||
int Add(int x,int y){
|
||||
return x + y;
|
||||
}
|
||||
int operator()(int x,int y){
|
||||
return x + y;
|
||||
}
|
||||
};
|
||||
void test01(){
|
||||
Complex complex;
|
||||
cout << complex.Add(10,20) << endl;
|
||||
//对象当做函数来调用
|
||||
cout << complex(10, 20) << endl;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test01();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// ============================================================================
|
||||
// p1c04_06_02 — 4.6.4 自增自减(++/--)运算符重载
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954458:1] 4.6.4 自增自减(++/--)运算符重载 ---
|
||||
class Complex{
|
||||
friend ostream& operator<<(ostream& os,Complex& complex){
|
||||
os << "A:" << complex.mA << " B:" << complex.mB << endl;
|
||||
return os;
|
||||
}
|
||||
public:
|
||||
Complex(){
|
||||
mA = 0;
|
||||
mB = 0;
|
||||
}
|
||||
//重载前置++
|
||||
Complex& operator++(){
|
||||
mA++;
|
||||
mB++;
|
||||
return *this;
|
||||
}
|
||||
//重载后置++
|
||||
Complex operator++(int){
|
||||
Complex temp;
|
||||
temp.mA = this->mA;
|
||||
temp.mB = this->mB;
|
||||
mA++;
|
||||
mB++;
|
||||
return temp;
|
||||
}
|
||||
//前置--
|
||||
Complex& operator--(){
|
||||
mA--;
|
||||
mB--;
|
||||
return *this;
|
||||
}
|
||||
//后置--
|
||||
Complex operator--(int){
|
||||
Complex temp;
|
||||
temp.mA = mA;
|
||||
temp.mB = mB;
|
||||
mA--;
|
||||
mB--;
|
||||
return temp;
|
||||
}
|
||||
void ShowComplex(){
|
||||
cout << "A:" << mA << " B:" << mB << endl;
|
||||
}
|
||||
private:
|
||||
int mA;
|
||||
int mB;
|
||||
};
|
||||
|
||||
void test(){
|
||||
Complex complex;
|
||||
complex++;
|
||||
cout << complex;
|
||||
++complex;
|
||||
cout << complex;
|
||||
|
||||
Complex ret = complex++;
|
||||
cout << ret;
|
||||
cout << complex;
|
||||
|
||||
cout << "------" << endl;
|
||||
ret--;
|
||||
--ret;
|
||||
cout << "ret:" << ret;
|
||||
complex--;
|
||||
--complex;
|
||||
cout << "complex:" << complex;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// ============================================================================
|
||||
// p1c04_06_03 — 4.6.5 指针运算符(*、->)重载
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954458:2] 4.6.5 指针运算符(*、->)重载 ---
|
||||
class Person{
|
||||
public:
|
||||
Person(int param){
|
||||
this->mParam = param;
|
||||
}
|
||||
void PrintPerson(){
|
||||
cout << "Param:" << mParam << endl;
|
||||
}
|
||||
private:
|
||||
int mParam;
|
||||
};
|
||||
|
||||
class SmartPointer{
|
||||
public:
|
||||
SmartPointer(Person* person){
|
||||
this->pPerson = person;
|
||||
}
|
||||
//重载指针的->、*操作符
|
||||
Person* operator->(){
|
||||
return pPerson;
|
||||
}
|
||||
Person& operator*(){
|
||||
return *pPerson;
|
||||
}
|
||||
~SmartPointer(){
|
||||
if (pPerson != NULL){
|
||||
delete pPerson;
|
||||
}
|
||||
}
|
||||
public:
|
||||
Person* pPerson;
|
||||
};
|
||||
|
||||
void test01(){
|
||||
|
||||
//Person* person = new Person(100);
|
||||
//如果忘记释放,那么就会造成内存泄漏
|
||||
|
||||
SmartPointer pointer(new Person(100));
|
||||
pointer->PrintPerson();
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
int main() {
|
||||
test01();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// ============================================================================
|
||||
// p1c04_06_01 — 4.6.2 运算符重载碰上友元函数
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
|
||||
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
|
||||
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
|
||||
// ============================================================================
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// --- 原文片段 [58954458:0] 4.6.2 运算符重载碰上友元函数 ---
|
||||
class Person{
|
||||
friend ostream& operator<<(ostream& os, Person& person);
|
||||
public:
|
||||
Person(int id,int age){
|
||||
mID = id;
|
||||
mAge = age;
|
||||
}
|
||||
private:
|
||||
int mID;
|
||||
int mAge;
|
||||
};
|
||||
|
||||
ostream& operator<<(ostream& os, Person& person){
|
||||
os << "ID:" << person.mID << " Age:" << person.mAge;
|
||||
return os;
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
Person person(1001, 30);
|
||||
//cout << person; //cout.operator+(person)
|
||||
cout << person << " | " << endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// --- main ---
|
||||
Reference in New Issue
Block a user