波奇学C++:类型转换和IO流

隐式类型转换

int i=0;
double p=i;

强制类型转换

int* p=nullptr;
int a=(int)p;

单参数构造函数支持隐式类型转换

class A
{
public:
	A(string a)
		:_a(a)
	{}
private:
	string _a;
};
A a("xxxx"); //"xxx" const char* 隐式转换为string

多参数也可以通过{}来实现隐式类型转换。

强制类型转换存在安全问题

const int n = 10;
int* p = (int*)&n;
(*p)++;
cout << n << endl; // 10 
cout << *p << endl; // 11

两个值不一样的原因是,*p是直接在内存中取值,而n没有去内存中取(没有确认改变),直接在寄存器中取。

解决方式:volatile 强制从内存中检查出关键字

volatile const int n = 10;

C++引入四种强制类型转换

static_cast 类型相关

//类型相关
int a = 10;
double b = static_cast<int>(a);

reinterpret_cast 类型不相关

// 类型不相关
int* p = &a;
int c = reinterpret_cast<int>(p);

const_cast 删除const属性,<type> 必须是指针或者引用

const int& d = 10;
int&e = const_cast<int&>(d);

dynamic_cast 父类对象的引用或者指针转换为子类对象

一般情况下C++允许向上转换,不允许向下转换

class A
{
public :
virtual void f(){}
};
class B : public A
{};
void fun (A* pa)
{
// pa 指向子类对象转换成功,转为父类对象返回Null
B* pb2 = dynamic_cast<B*>(pa); 
cout<<"pb2:" <<pb2<< endl;
}
int main ()
{
  B b;
  fun(&b);
  return 0;
}
1. dynamic_cast只能用于父类含有虚函数的类
2. dynamic_cast会先检查是否能转换成功,能成功则转换,不能则返回0

 I/O流

I/O流

能更好的打印自定义类型

自动识别类型

运算符重载

 cin对象类型转换

int main()
{
	string str;
	while (cin >> str)
	{
		cout << str << endl;
	}
	return 0;
}

 cin>>str 返回值是cin, cin作为while的判断对象。

下面的的类型重载可以让cin隐式转为bool 值

class A
{
public:
	A(int a=0)
		:_a(a)
	{}
	operator int()//允许转换成int型
	{ 
		return _a;
	}
	operator bool()//允许转换成bool类型
	{
		return _a;
	}
	int _a;
};
A a=10;
int i=a;
bool ii =a;

operator bool()将cin转换成bool值,来作为判断while是否结束。

C++文件IO流

class Date
{
	friend ostream& operator << (ostream& out, const Date& d);
	friend istream& operator >> (istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	operator bool()
	{
		// 这里是随意写的,假设输入_year为0,则结束
		if (_year == 0)
			return false;
		else
			return true;
	}
private:
	int _year;
	int _month;
	int _day;
};
istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
ostream& operator << (ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day;
	return out;
}

二进制写入文件

int main()
{
	Date d(2013, 10, 14);
	FILE* fin = fopen("file.txt", "w");
	fwrite(&d, sizeof(Date), 1, fin);
	fclose(fin);
	return 0;
}

 文件是字节流,二进值的无法正常显示

C++:二进制写入

Date d(2013, 10, 14);
ofstream ofs("file.txt",ios_base::out|ios_base::binary);
ofs.write((const char*)&d, sizeof(d));

按文本的方式写

Date d(2013, 10, 14);
ofstream ofs("file.txt",ios_base::out|ios_base::binary);
ofs << d;

调用流插入重载函数。ofs继承out

 二进制读写不能用string,vector这样的对象存数据,否则写出去就是个指针,进程结束就是野指针

流插入时注意空格分隔,cin读取时会默认用空格进行分割 

读取文件

ifstream ifs("源.cpp");
char ch;
while (ifs.get(ch))
{
	cout << ch;
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值