摘要: 1 问题 C++ 如何清理需要销毁的对象? 2 对象的销毁 一般而言,需要销毁的对象都应该做清理 解决方案 为每个类都提供一个 public 的 free 函数 对象不再需要时立即调用 free 函数进行清理 class Test { int* p; public: Test(){ p = new 阅读全文
posted @ 2020-09-23 13:23 nxgy 阅读(137) 评论(0) 推荐(0) 编辑
摘要: 1 问题 C++ 中的类可以定义多个对象,那么对象构造的顺序是怎样的? 2 对象的构造顺序 对于局部对象 当程序执行流到达对象的定义语句时进行构造 示例:局部对象的构造顺序 Demo #include <stdio.h> class Test { private: int mi; public: T 阅读全文
posted @ 2020-09-23 12:30 nxgy 阅读(143) 评论(0) 推荐(0) 编辑
摘要: 1 问题引入 问题:类中是否可以定义 const 成员? 示例:下面的类定义是否合法?如果合法,ci 的值是什么,存储在哪里? Demo #include <stdio.h> class Test { private: const int ci; public: int getCI() { retu 阅读全文
posted @ 2020-09-22 23:38 nxgy 阅读(105) 评论(0) 推荐(0) 编辑
摘要: 1 对象的初始化 问题:对象中成员变量的初始值是多少? Demo #include <stdio.h> class Test { private: int i; int j; public: int getI() { return i; } int getJ() { return j; } }; T 阅读全文
posted @ 2020-09-22 22:04 nxgy 阅读(108) 评论(0) 推荐(0) 编辑
摘要: 1 const 问题:const 所修饰的标识符,什么时候为只读变量,什么时候是常量 const 常量的判别准则 只有用字面量初始化的 const 常量才会进入符号表 使用其他变量初始化的 const 常量仍然是只读变量 被 volatile 修饰的 const 常量不会进入符号表 在编译期间不能直 阅读全文
posted @ 2020-09-21 23:43 nxgy 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 1 C 语言中的强制类型转换 C 方式的强制类型转换 (Type)(Expression) Type(Expression) 示例:粗暴的类型转换 Demo #include <stdio.h> typedef void(PF)(int); //定义一个函数类型 struct Point { int 阅读全文
posted @ 2020-09-21 21:44 nxgy 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 1 动态内存分配 C++ 中的动态内存分配 C++ 中通过 new 关键字进行动态内存分配 C++ 中的动态内存申请是基于类型进行的 delete 关键字用于内存释放 //变量申请 Type* pointer = new Type; //... delete pointer; //数组申请 Type 阅读全文
posted @ 2020-09-21 21:42 nxgy 阅读(115) 评论(0) 推荐(0) 编辑
摘要: 1 C++ 中的函数重载 函数重载(Function Overload) 用同一个函数名定义不同的函数 当函数名和不同的参数搭配时函数的含义不同 示例 Demo #include <stdio.h> #include <string.h> int func(int x) { return x; } 阅读全文
posted @ 2020-09-21 21:40 nxgy 阅读(171) 评论(0) 推荐(0) 编辑
摘要: 1 函数参数的默认值 C++ 中可以在函数声明时为参数提供一个默认值 当函数调用时没有提供参数的值,则使用默认值 int mul(int x = 0); int main(int argc,char* argv[]) { printf("%d\n",mul()); // <=> mul(0) ret 阅读全文
posted @ 2020-09-21 21:39 nxgy 阅读(82) 评论(0) 推荐(0) 编辑
摘要: 1 常量与宏回顾 C++ 中的 const 常量可以替代宏常数定义,如: const int A = 3; <=> #define A 3 问题:C++ 中是否有解决方案替代宏代码片段? 2 内联函数 C++ 中推荐使用内联函数替代宏代码片段 C++ 中使用 inline 关键字声明内联函数 内联函 阅读全文
posted @ 2020-09-19 16:52 nxgy 阅读(127) 评论(0) 推荐(0) 编辑