这一个月搬家,没有网络,文章好久没更新了,现在贴出c++学习的最后一片。
一、使用c++中的虚函数表
1 // 取出来虚函数表中的函数,类型重定义
2 #include<iostream>
3 using namespace std;
4 class A
5 {
6 public:
7 virtual void fooa(int x)
8 {
9 cout << "fooa(int)" << endl;
10 cout << x << endl;
11 }
12 virtual void foob(int x)
13 {
14 cout << "foob(int)" << endl;
15 cout << x << endl;
16 }
17
18 };
19 typedef void (*VFUN)(A* mythis,int x); // 按理说,typedef void (*)(A* mythis,int x) VFUN;为什么不能这样呢,不幸的是,如此typedef就有三节,
20 // 这样编译器就没办法理解了,于是语法规定,将名字置于*后面,保持两段的结构。
21 typedef VFUN* VTABLE; // 这样VTABLE从字面上看就是一级指针表示二级指针了
22 int main()
23 {
24 A a;
25 VTABLE vt = *((VTABLE*)&a);
26 vt[0](&a,123);
27 vt[1](&a,456);
28 vt[2](&a,789);
29
30 return 0;
31 }
~
二、c++中的标准对象
cout
cin
cerr
clog
1 // 有一个叫做system的函数,可以执行shell的各种指令
2 #include<iostream>
3 #include<cstdlib>
4 using namespace std;
5 int main()
6 {
7 cout << "hello " << flush; // cout的东西能进行重定向
8 clog << "test " << flush; // 和上一行进行交换之后,得到的输出结果匪夷所思,原因在于cout具有缓冲区,除非遇到换行
9 cerr << "world "; // cerr不需要缓冲,cerr和clog的东西不能进行重定向
10 cout << endl;
11 system("ls -l");
12 return 0;
13 }
缓冲 flush
cout 可以重定向
cerr和clog不能重定向。
find / -name 01vtable.cpp 这是linux下查找文件的方法, find / -name 01vtable.cpp 2>/dev/null,2表示错误,整句话表示找01vtable.cpp文件,错误扔
进空设备中。
三、IO流的类层次图,涉及下面三个头文件
istream ostream #include<iostream>
ifstream ofstream #include<fstream> // 文件输入输出
fstream
istringstream
ostringstream #include<sstream> // 字符串的输入输出
四、 格式化IO和非格式化IO(format)
格式化有类型的概念(c语言中的scanf,和printf都是格式化输入输出,%d格式,%s格式,%c格式等等,c++中是自动识别的)
int x;
cin >> x;
cout << x;
非格式化:没有类型的概念(c语言中有getchar,puts等)
所有的数据都当成字符来看
格式化会自动空被,而非格式化不会。
五、非格式化
1 get用来得到一个字符,重载了很多中形式,下面列举两种
int get(); // EOF代表结束,但是不是文件的内容,它是一个宏
istream& get(char& ch) ;// 只要流变成NULL,就代表文件结束了
ostream& put(char ch)
istream& getline(char* buffer,streamsize num); 最多读取num-1个字符,遇到换行立即结束。
istream& getline(char* buffer,streamsize num,char delim); 遇到指定的字符delim才结束