Android NDK基础23:C++_类型转换_IO流_对象持久化

C++类型转换

C++类型转换

  • static_cast 普遍情况
  • const_cast 去常量
  • dynamic_cast 子类类型转为父类类型
  • reinterpret_cast 函数指针转型,不具备移植性

static_cast 普遍情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

using namespace std;

//原始类型转换,所有情况都是一种写法,可读性不高,有可能有潜在的风险

void* func(int type){
switch (type){
case 1: {
int i = 9;
return &i;
}
case 2: {
char a = 'X';
return &a;
}
default:{
return NULL;
}
}
}

void func2(char* c_p) {
cout << *c_p << endl;
}

void main() {
//int i = 0;
//自动转换
//double d = i;
//double d = 9.5;
//int i = d;

//int i = 8;
//double d = 9.5;
//i = static_cast<int>(d);

//void* -> char*
//char* c_p = (char*)func(2);
//char* c_p = static_cast<char*>(func(2));

//C++ 意图明显
func2(static_cast<char*>(func(2)));
//C
func2((char*)(func(2)));

system("pause");
}

const_cast 去常量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void func(const char c[]) {
//c[1] = 'a'; //不可行
//通过指针间接赋值
//其他人并不知道,这次转型是为了去常量
//char* c_p = (char*)c;
//c_p[1] = 'X';
//提高了可读性
char* c_p = const_cast<char*>(c);
c_p[1] = 'Y';

out << c << endl;
}

void main() {
char c[] = "hello";
func(c);

system("pause");
}

dynamic_cast 子类类型转为父类类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Person {
public:
virtual void print() {
cout << "人" << endl;
}
};

class Man : public Person {
public:
void print() {
cout << "男人" << endl;
}

void chasing() {
cout << "泡妞" << endl;
}
};

class Woman : public Person {
public:
void print() {
cout << "女人" << endl;
}

void carebaby() {
cout << "生孩子" << endl;
}
};

void func(Person* p) {
//调用子类的特有的函数,转为实际类型
//并不知道转型失败
//Man* m = (Man*)p;
//m->print();

//转型失败,返回NULL
Man* m = dynamic_cast<Man*>(p);
if (m != NULL) {
m->chasing();
}

Woman* w = dynamic_cast<Woman*>(p);
if (w != NULL) {
w->carebaby();
}
}

void main() {
//Woman w;
//Person *p = &w;

//func(p);

system("pause");
}

reinterpret_cast 函数指针转型,不具备移植性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void func1() {
cout << "func1" << endl;
}

char* func2() {
cout << "func2" << endl;
return "abc";
}

typedef void(*f_p)();

void main() {
//函数指针数组
f_p f_array[6];
//赋值
f_array[0] = func1;

//C方式
//f_array[1] = (f_p)(func2);
//C++方式
f_array[1] = reinterpret_cast<f_p>(func2);

f_array[1]();

system("pause");
}

IO流

文本文件操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <fstream>

//IO流
//文本文件操作
void main() {
char* fname = "c://dest.txt";
//输出流
ofstream fout(fname);
//创建失败
if (fout.bad()) {
return;
}

fout << "Hello" << endl;
fout << "World" << endl;

//关闭
fout.close();

//读取
ifstream fin(fname);
if (fin.bad()) {
return;
}
char ch;
while (fin.get(ch)) {
//输出
cout << ch;
}
fin.close();

system("pause");
}

二进制文件操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//二进制文件
void main() {
char* src = "c://src.jpg";
char* dest = "c://dest.jpg";

//输入流
ifstream fin(src, ios::binary);
//输出流
ofstream fout(dest, ios::binary);

if (fin.bad() || fout.bad()) {
return;
}

while (!fin.eof()) {
//读取
char buff[1024] = {0};
fin.read(buff, 1024);

//写入
fout.write(buff, 1024);
}

//关闭
fin.close();
fout.close();

system("pause");
}

C++对象的持久化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//C++对象的持久化
class Person {
public:
Person() {

}

Person(char * name, int age) {
this->name = name;
this->age = age;
}

void print() {
cout << name << "," << age << endl;
}

private:
char *name;
int age;
};

void main() {
Person p1("柳岩", 22);
Person p2("Joe", 18);
//输出流
ofstream fout("c://c_obj.data", ios::binary);
fout.write((char*)(&p1), sizeof(Person)); //指针能够读取到正确的数据,读取内存区的长度
fout.write((char*)(&p2), sizeof(Person));
fout.close();

//输入流
ifstream fin("c://c_obj.data", ios::binary);
Person tmp;
fin.read((char*)(&tmp), sizeof(Person));
tmp.print();

fin.read((char*)(&tmp), sizeof(Person));
tmp.print();

system("pause");
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :