当前位置: 首页 > news >正文

b2c平台网址滕州网站建设优化

b2c平台网址,滕州网站建设优化,扬中炒地皮,网站地址格式隐式类型转换 int i0; double pi; 强制类型转换 int* pnullptr; int a(int)p; 单参数构造函数支持隐式类型转换 class A { public:A(string a):_a(a){} private:string _a; }; A a("xxxx"); //"xxx" const char* 隐式转换为string 多参数也可以通过{…

隐式类型转换

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;elsereturn 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;
}

http://www.wangmingla.cn/news/47205.html

相关文章:

  • 网站建设的关键词百度官网地址
  • 凡科网上商城seo赚钱暴利
  • 长沙网站制作案例长沙网站快速排名提升
  • 静态网站怎么样苏州排名搜索优化
  • 用空间做网站如何做好安全网络营销方案怎么写
  • 贵阳培训网站建设网络营销推广方案前言
  • 镇江网站建设远航科技必应bing国内版
  • 网站设计师发展前景南京seo关键词排名
  • 有哪些做简历的网站广告主广告商对接平台
  • 用avada做网站首页360优化大师旧版本
  • 宿迁网站建设费用百度上看了不健康的内容犯法吗
  • 北京网站制作net2006实时热点新闻事件
  • iis wordpress固定链接404上海seo公司
  • 网站建设活动怎样建立自己网站
  • 杨凌做网站世界羽联巡回赛总决赛
  • 自己造网站友情连接出售
  • 聊城网站建设哪家便宜江苏百度推广代理商
  • 心理咨询网站开发营销最好的方法
  • 购物网站备案费用中国培训网官网
  • 站长工具seo综合查询隐私查询页面设计漂亮的网站
  • 做企业网站赚钱吗西地那非片
  • 西安网站建设招骋杭州网站seo推广
  • 长春做网站建设的公司seo怎么读
  • 网站主题如何制作公司推广网站
  • 网站响应式技术自建网站
  • 公司网站的具体步骤优化网站排名
  • 家居类企业响应式网站关键词seo排名优化软件
  • b站晚上少人不宜seo和sem是什么意思
  • 做外贸一般在哪个网站网址如何被快速收录
  • 东莞网站建设多少钱化工seo顾问