13b3ebbe14
把川大 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 运行通过。
84 lines
2.2 KiB
C++
84 lines
2.2 KiB
C++
// ============================================================================
|
|
// 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;
|
|
}
|