c++
cplusplus
我一定会吃下怪人细胞
这个作者很懒,什么都没留下…
展开
-
this指针
1.this指针的引入 class Date { public: void Show() { cout << _year << "-" << _month << "-" << _day << endl; } void Set(int year, int month, int day) { _year = year; _month = month; _day = day; } private: int _ye原创 2020-06-27 11:28:17 · 91 阅读 · 0 评论 -
指针空值nullptr
1.C++98中的指针空值 声明一个变量时最好给该变量一个合适的初始值,否则可能会出现不可预料的错误,比如未初始化的指针。如果一个指针没有合法的指向,我们基本都是按照如下方式对其进行初始化: void TestPtr() { int* p1 = NULL; int* p2 = 0; } NULL实际是一个宏,在传统的C头文件(stddef.h)中,可以看到如下代码 #ifndef NULL #ifdef __cplusplus #define NULL 0 #else #define NULL ((v原创 2020-06-26 15:29:00 · 595 阅读 · 0 评论 -
基于范围的for循环
1.范围for的语法 在C++98中如果要遍历一个数组,可以按照以下方式进行: void TestFor() { int array[] = { 1, 2, 3, 4, 5 }; for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i) array[i] *= 2; for (int* p = array; p < array + sizeof(array)/ sizeof(array[0]); ++p) cout << *原创 2020-06-26 14:44:53 · 268 阅读 · 0 评论 -
auto关键字
1. auto简介 在早期C/C++中auto的含义是:使用auto修饰的变量,是具有自动存储器的局部变量,C++11中,标准委员会赋予了auto全新的含义即auto不再是一个存储类型指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得 int TestAuto() { return 10; } int main() { int a = 10; auto b = a; auto c = 'a'; auto d = TestAuto(); cout <<原创 2020-06-26 14:27:20 · 140 阅读 · 0 评论 -
引用
引用的概念 引用的特性 常引用 使用场景 传值、传引用效率比较 引用和指针的区别 1.引用的概念 引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同一块内存空间。 类型& 引用变量名(对像名)=引用实体 void TestRef() { int a = 10; int& ra = a; //<--定义引用类型 cout << a << endl; cout << ra <.原创 2020-06-26 12:27:18 · 107 阅读 · 0 评论