c++primer plus笔记
2024-09-04 12:36:21
第六版
操作符重载
#include<iostream>
using namespace std;
class Time
{
public:
Time()
{
h=m=0;
}
Time(int _h,int _m)
{
h = _h;
m = _m;
}
void show()
{
printf("%02d:%02d \n",h,m);
}
Time operator+(const Time &t)
{
Time result;
result.m = t.m + m;
result.h = t.h + h + result.m/60;
result.m %= 60;
return result;
}
private:
int h;
int m;
};
int main(int argc,char* argv[])
{
Time t(3,45);
Time c(3,15);
Time w = t + c;
w.show();
system("pause");
return 0;
}
const总结
#include<iostream>
using namespace std;
class Demo
{
public:
int x;
Demo(int _x):x(_x){}
void testConstFunction(int _x) const{
///错误,在const成员函数中,不能修改任何类成员变量
x=_x;
///错误,const成员函数不能调用非onst成员函数,因为非const成员函数可以会修改成员变量
modify_x(_x);
}
void modify_x(int _x){
x=_x;
}
};
int main(int argc,char* argv[])
{
int a = 5;
int b = 10;
const int * p1 = &a; //常数据,不能通过解引用修改数据
int const * p4 = &a; //常数据
int * const p2 = &b; //常指针
const int * const p3 = &a; //常数据和常指针
// p2 = &a; //error
//*p1 = 1; //error
p1 = &b; //ok
system("pause");
return 0;
}
存储持续性
自动存储持续性
函数和代码块中的变量
静态存储持续性
函数外定义或static的变量
动态存储持续性
使用new分配的内存一直存在
线程存储持续性
c++11,变量使用thread_local定义时
最新文章
- USACO3.1Humble Numbers[...]
- php 安装composer
- Ubuntu 12.04 禁用触摸板
- Working with Other Node Types
- Quartz所使用的表的说明
- Intellij IDEA 创建消息驱动Bean - 接收JMS消息
- 在Linux(Ubuntu/openSUSE/CentOS)下配置ASP.NET(Apache + Mono)转载+补充
- unix 环境高级编程-读书笔记与习题解答-第二篇
- [Powershell] FTP Download File
- Lucene学习总结之七:Lucene搜索过程解析
- python对真假的判断方式
- SQL随机查询,显示行号,查询数据段
- Lucene 4.4 依据Int类型字段删除索引
- lock table
- Testing a Redux &; React web application
- TV端:通过遥控器的点击实现图片的上下左右抖动的效果
- RHL 6.0学习日记, 先记下来,以后整理。
- Hi,腾讯WeTest联合Unity官方打造的性能分析工具UPA,今日全新发布!
- struts2简单入门-Action的三种配置方式
- Unity打开外部程序exe/Bat文件方案