当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++类型转换代码实例

C++类型转换代码实例

2018年09月12日  | 移动技术网IT编程  | 我要评论

秒杀 淘宝,儿童歌曲视频,随身空间之周芷若

c++类型转换代码实例

#include
using namespace std;

//c++类型转换
//static_cast 普遍情况
//const_cast 去常量
//dynamic_cast 子类类型转为父类类型
//reinterpret_cast 函数指针转型,不具备移植性
//原始类型转换,所有情况都是一种写法,可读性不高,有可能有潜在的风险
/*void* fun(int type){
 switch (type)
 {
 case 1:{
int a = 4;
return &a;
//break;
 }
 case 2:{
char b = 'c';
return &b;
//break
 }
 default:{
return null;
//break
 }
 }
}

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

void main(){
 //自动转换
 double a = 23.44;
 int c = a;//这里自动将double类型转变成int类型
 int d = static_cast(a);//这里强调了转换
 cout << c << endl;

 //这是c的方式
 char* c_p=(char*)fun(2);
 //c++方式 意图更加明显
 char* c_p2 = static_cast(fun(2));

 fun2((char*)fun(2));
 fun2(static_cast(fun(2)));

 system("pause");
}*/

/* const_cast 去常量
void fun(char c[]){
 //通过指针间接赋值
 //其他人并不知道,这次转型是为了去常量
 char* c_p = (char*)c;

 //c++方式转型,提高了可读性
 char* c_p2 = const_cast(c);

 //修改数组的第二个字母
 c_p[1] = 'x';

 cout << c_p << endl;
}

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

 system("pause");
}*/

//dynamic_cast 子类类型转为父类类型
/*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 body(){
  cout << "生孩子" << endl;
 }
};

void fun(person* p){
 //1.
 //调用子类的特有的函数,转为实际类型
 //这里就会有问题,如果传递过来的是man没有问题,如果是woman就会发生转换类型错误
 //man* man = (man*)p;

 //man->print();
 //man->chasing();

 //2.使用c++的强转  如果转换不成功则为null
 man* man2 = dynamic_cast(p);
 if (man2!=null)
 {
  man2->print();
  man2->chasing();
 }
 woman* wm = dynamic_cast(p);
 if (wm != null)
 {
  wm->print();
  wm->body();
 }
}

void main(){
 //1.
 //man m1;
 //person* p1 = &m1;
 //woman wm1;
 //person* p2 = &wm1;
 //fun(p1);男人 泡妞
 //fun(p2); 女人  泡妞 有问题


 //2.
 man m1;
 person* p1 = &m1;

 woman wm1;
 person* p2 = &wm1;
 fun(p1);
 fun(p2);

 system("pause");
}*/

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

//函数指针
/*typedef void(*fun)();

void fun1(){
 cout << "fun1" << endl;
}

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

void main(){
 fun fun_arr[6];
 fun_arr[0] = fun1;
 //函数转成函数指针
 fun_arr[1] = reinterpret_cast(fun2);

 system("pause");
}*/

//io流
//文本的输入和输出
#include
/*
void main(){
 char* path = "d://a.txt";
 //输出流
 ofstream fout(path);
 //判断是否有文件
 if (fout.bad())
 {
  return;
 }
 //写入数据
 fout << "hello" << endl;
 fout << "json" << endl;
 //关闭流
 fout.close();

 //输入流
 ifstream fin(path);
 //是否正确读取
 if (fin.bad())
 {
  return;
 }
 //读文件
 char ch;
 while (fin.get(ch)){
  cout << ch << endl;
 }
 //关闭流
 fin.close();
 system("pause");
}*/

//二进制文件
/*
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++对象的持久化
/*
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("d://c_obj.data", ios::binary);
 //指针能够读取到正确的数据,读取内存区的长度
 fout.write((char*)(&p1), sizeof(person)); 
 fout.write((char*)(&p2), sizeof(person));
 fout.close();

 //输入流
 ifstream fin("d://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");
}*/

//stl standard template library 标准模板库
//util
//c++ 集合-> java 集合

#include 

/*
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);


 system("pause");
}
*/
//容器
#include 

void main()
{
 //动态数组
 //不需要使用动态内存分配,就可以使用动态数组
 vector 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;
 }

 system("pause");
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网