待学习的语法和兴趣点记录

1、<<和>>的重载为什么要用友元函数,普通的重载为何不可?

友元函数概念尚未学习

 2、拷贝构造访问类对象私有变量

https://www.cnblogs.com/dwdxdy/archive/2012/07/17/2595741.html

 3、c++关键字

4、初始化和赋值的差异

列表初始化

5、复合类型:指针和引用,引用定义必须赋初值,且必须是对象

指针的引用  int *p;int *&r = p;

const和指针:指向常量的指针和常量指针;

const和引用:double i=3.14;const int &r = i *2(合法,绑定到了一个临时int型对象上);int &r = i *2(不合法,类型不一致,不是const就可能变化,那应当修改原对象,而不是一个临时对象,故而报错)

类型别名:typedef char *p(p表示char *);using int4=int(int4表示int型);

auto类型定义时必须有初始值:auto i=0 ,*p=&i;同一行的变量类型必须一致,比如 auto i=0, i1=3.14;就是错的。

decltype:选择并返回操作数的数据类型。

auto和decltype:

auto会忽略顶层const,保留底层const,decltype保留所有顶层和底层const。

const int i=0,&ri=i;auto b=i(b是一个整数);auto c=ri(c是一个整数);auto d=&i(d是一个指向int常量的指针);整个例子将i当成顶层const看。

auto推断引用是根据引用对象的数据类型而定,而decltype则返回r是int &,后面必须紧跟初始化;

int i=0, &r=i;auto at= r;   decltype(r) dt=i; 

 

posted @ 2021-04-03 16:57  loveforthee  阅读(67)  评论(0)    收藏  举报