C++类型转换

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

//原始类型转换, 所有情况都是一种写法,可读性不高,有可能有潜在的风险
//static_cast
#include<iostream>
using namespace std;

void* func(int type) {
	switch (type)
	{
	case 1:
		int i = 9;
		return &i;
	case 2:
		int 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.2;
	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)));
	getchar();
}
//const_cast
#include<iostream>
using namespace std;

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';
	cout << c << endl;
}

void main() {
	char c[] = "hello";
	func(c);
	getchar();
}
//dynamic_cast
#include<iostream>
using namespace std;

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* obj) {
	//调用子类的特有的函数,转为实际类型
	//并不知道转型失败
	//Man* m = (Man*)obj;
	//m->print();

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

void main() {
	Woman w1;
	Person *p1 = &w1;
	func(p1);
	//Woman w1;
	//Woman* w_p = &w1;
	
	getchar();
}
//reinterpret_cast
#include<iostream>
using namespace std;

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]();
	getchar();
}
//IO流
//文本文件操作
#include <fstream>
#include<iostream>
using namespace std;

void main() {
	char* fname = "c://dest.txt";
	//输出流
	ofstream fout(fname);
	//创建失败
	if (fout.bad()) {
		return;
	}
	fout << "jack" << endl;
	fout << "rose" << endl;

	//关闭
	fout.close();

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

}
//二进制文件
void main() {
	char* src = "c://drop.png";
	char* dest = "c://dest.png";
	//输入流
	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();

	getchar();
}
//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("rose", 18);
	//输出流
	ofstream fout("c://c_obj.data", ios::binary);
	fout.write((char*)(&p1),sizeof(Person));//指针能够读取到正确的数据,读取内存区的长度
	fout.write((char*)(&p2), sizeof(Person));

	//输入流
	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();
	getchar();
}
//stl  standard template library
//util
//c++ 集合-->java 集合
#include<string>

void main() {
	string s1 = "craig david";
	string s2(" 7 days");
	string s3 = s1 + s2;
	cout << s3 << endl;
	//转C字符串
	const char* c_str = s3.c_str();
	cout << c_str << endl;
	//s1.at(2);
	getchar();
}
//容器
//动态数组
//不需要使用动态内存分配,就可以使用动态数组
#include <vector>

void main() {
	vector<int> v;
	v.push_back(12);
	v.push_back(10);
	v.push_back(5);
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << endl;
	}
	getchar();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值