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

做暧暧视频网站在线百度有刷排名软件

做暧暧视频网站在线,百度有刷排名软件,网站建设大宇,自己免费做网站(三)C中的类型转换(Type Conversion)是指将一个数据类型的值转换为另一个数据类型的过程,主要包括: 一、算术类型转换(Arithmetic Conversions) 算术类型转换通常发生在算术运算或比较中,称为**“标…

C++中的类型转换(Type Conversion)是指将一个数据类型的值转换为另一个数据类型的过程,主要包括:


一、算术类型转换(Arithmetic Conversions)

算术类型转换通常发生在算术运算或比较中,称为**“标准转换”(standard conversion),遵循一定的提升规则(promotion rules)**。

转换规则(简化版):

  1. boolint
  2. 整型提升:charshortint
  3. 两个操作数不同类型时:
    • 若一个为浮点型,另一个为整型 → 整型转换为浮点型
    • 若一个为double,另一个为floatfloat 转为 double
    • 若一个为unsigned且它的值不能用int表示 → int转为unsigned
    • 否则,unsigned提升优先级高于signed

示例:

#include <iostream>
int main() {char c = 100;int i = 200;float f = 1.5f;double d = 3.14;auto result1 = c + i;   // char → int,结果是 intauto result2 = i + f;   // int → float,结果是 floatauto result3 = f + d;   // float → double,结果是 doublestd::cout << "result1: " << result1 << "\n"; // 300std::cout << "result2: " << result2 << "\n"; // 201.5std::cout << "result3: " << result3 << "\n"; // 4.64
}

二、其他隐式类型转换(Implicit Conversion)

这是 编译器自动完成 的转换,也叫类型提升(promotion)或类型协同(coercion)

常见的隐式转换:

类型转换方向示例
数值型更大/更精度的数值intdouble
指针类型派生类 → 基类Derived*Base*
空指针nullptr → 任何指针类型nullptr → int*
数组/函数 → 指针int arr[]int*

示例:

void printDouble(double d) {std::cout << "Double: " << d << "\n";
}class Base {};
class Derived : public Base {};int main() {int i = 42;printDouble(i); // int → double(隐式)Derived d;Base* bptr = &d; // Derived* → Base*(隐式)
}

三、显示类型转换(Explicit Conversion / Cast)

你可以显式地指示编译器进行类型转换,有四种标准C++风格的转换方式(推荐),以及一个C风格的转换(不推荐)。

四种C++显式转换:

  1. static_cast<T>(expr)
    ➤ 用于基本类型之间转换指针/引用向上转换等安全转换。

  2. dynamic_cast<T>(expr)
    ➤ 用于带有虚函数的多态类之间的安全运行时类型检查和向下转换

  3. const_cast<T>(expr)
    添加或去除const、volatile限定符,常用于函数参数处理。

  4. reinterpret_cast<T>(expr)
    不安全但允许的位级别转换,如指针与整数之间转换。

  5. (T)expr —— C风格强制转换
    ➤ 组合了上述所有功能,不安全不透明,不推荐使用


示例:

#include <iostream>
class Base { public: virtual ~Base() {} };
class Derived : public Base { public: void say() { std::cout << "Derived\n"; } };int main() {int i = 100;double d = static_cast<double>(i);  // int → doubleBase* b = new Derived();Derived* dptr = dynamic_cast<Derived*>(b);  // 安全向下转型if (dptr) dptr->say();const int ci = 42;int* p = const_cast<int*>(&ci);  // 去除 const 限定void* vptr = reinterpret_cast<void*>(p); // int* → void*int* iptr = reinterpret_cast<int*>(vptr); // void* → int*delete b;
}

四、总结对比表:

转换类型触发方式安全性用途/说明
算术转换自动安全数值运算中的标准提升
隐式转换自动通常安全函数调用/指针/基本类型提升
static_cast显式安全编译期可检查的转换,如 int→float,向上转型
dynamic_cast显式安全多态类型的运行时向下转型
const_cast显式有风险去除 const/volatile
reinterpret_cast显式不安全位级别转换,极端情况用
C风格强制转换显式不推荐混合多个C++转换,缺乏类型安全

五、综合例子

下面是一个C++ 示例工程,通过一个模拟图形系统的类结构,演示了**各种类型转换(隐式、算术、显示转换)**的组合使用。代码里有注释,方便理解每一种转换在什么时候触发、为什么安全或者不安全。

定义 shape.h图形类头文件内容如下:

#pragma once
#include <iostream>
#include <string>class Shape {
public:virtual ~Shape() {}virtual void draw() const;
};class Circle : public Shape {
public:Circle(double r);void draw() const override;double getRadius() const;
private:double radius;
};class Rectangle : public Shape {
public:Rectangle(int w, int h);void draw() const override;int area() const;
private:int width, height;
};// 模拟图形渲染系统的底层资源对象
struct RenderHandle {void* data;
};RenderHandle createHandle(int id);

shape.cpp 图形类实现如下:

#include "shape.h"void Shape::draw() const {std::cout << "Drawing Shape\n";
}Circle::Circle(double r) : radius(r) {}
void Circle::draw() const {std::cout << "Drawing Circle with radius: " << radius << "\n";
}
double Circle::getRadius() const {return radius;
}Rectangle::Rectangle(int w, int h) : width(w), height(h) {}
void Rectangle::draw() const {std::cout << "Drawing Rectangle of area: " << area() << "\n";
}
int Rectangle::area() const {return width * height;
}RenderHandle createHandle(int id) {RenderHandle h;h.data = reinterpret_cast<void*>(id);  // 显式 reinterpret_castreturn h;
}

main.cpp 主函数演示各种类型转换示例内容如下:

#include "shape.h"
#include <typeinfo>void printDouble(double d) {std::cout << "Double value: " << d << "\n";
}int main() {// 算术类型转换(int → double)int i = 42;double d = i; // 隐式转换printDouble(d); // 参数是 doublefloat f = static_cast<float>(i); // 显式 static_caststd::cout << "Float from int: " << f << "\n";// 基类 → 派生类的动态转换(dynamic_cast)Shape* s = new Circle(3.5);s->draw();if (Circle* c = dynamic_cast<Circle*>(s)) {std::cout << "Radius from casted circle: " << c->getRadius() << "\n";}// const_cast 演示const int ci = 100;int* modifiable = const_cast<int*>(&ci);*modifiable = 999; // 未定义行为,仅演示用std::cout << "Modified const int (unsafe!): " << *modifiable << "\n";// reinterpret_cast 演示int resourceID = 1234;RenderHandle h = createHandle(resourceID);int recoveredID = reinterpret_cast<int>(h.data);std::cout << "Recovered ID from RenderHandle: " << recoveredID << "\n";// 隐式派生 → 基类(安全)Rectangle rect(10, 20);Shape* s2 = &rect;s2->draw();  // 虚函数,多态行为delete s;return 0;
}

示例的演示内容如下:

场景类型转换涉及关键字/方式
int → double算术隐式转换自动
float ← int算术显式转换static_cast
Base* ← Derived*隐式向上转换自动
Derived* ← Base*安全运行时转换dynamic_cast
const int* ← int*添加/去除 constconst_cast
void* ←→ int指针/整数转换reinterpret_cast
Shape 多态行为虚函数机制隐式
http://www.dtcms.com/wzjs/158991.html

相关文章:

  • 我想建立个网站怎么弄培训机构招生方案
  • 重庆营销网站制作商丘关键词优化推广
  • 做移动端网站软件长沙网站推广排名优化
  • 如何提高网站点击率营销说白了就是干什么的
  • 网站建设方案书备案全国疫情高峰感染高峰
  • 网站建设与网页制作关键词优化怎么写
  • 广州网站建设类岗位真正免费的网站建站平台
  • 做网站到哪里做外贸网络营销推广
  • 网站通内容管理系统企业宣传方式
  • 网站建设制作深圳seo技术培训山东
  • 自己做视频的网站线上宣传渠道和宣传方式
  • 抚州企业网站做优化搜狗网站收录提交入口
  • 企业网站建设需要考虑内容seo是什么seo怎么做
  • 织梦做的网站快照被攻击跨境电商营销推广
  • 做视频直播网站北京企业网络推广外包
  • 网站排名必做阶段性seo策略产品推广公司
  • 百度 搜索到手机网站seo管理是什么
  • 可信网站身份验证郑州高端网站制作
  • wordpress主题里加广告专业放心关键词优化参考价格
  • 网站上做扫一扫share群组链接分享
  • 软件外包公司容易进吗杭州seo中心
  • wordpress 订单管理广州抖音seo公司
  • 织梦做博客类网站数字营销公司排行榜
  • 沧州网站建设专业定制手机网站百度关键词排名
  • 商城网站模板免费广告发布平台app
  • 做网站 用哪个网盘好关键词网络推广企业
  • 上海市电话黄页本东莞百度seo电话
  • 网站左侧导航代码化工网站关键词优化
  • 嘉定网站设计制作报价以图搜图百度识图网页版
  • 长春优惠做网站关键词优化公司哪家好