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

下载什么网站做吃的百度一下官网搜索引擎

下载什么网站做吃的,百度一下官网搜索引擎,企业宣传ppt模板,重庆行业平台C 类型转换 包括C风格的转换、static_cast、const_cast、reinterpret_cast、dynamic_cast、模板特化等 flyfish 0. 隐式转换(Implicit Conversions) 隐式转换是编译器自动进行的类型转换,通常在需要将一个类型转换为另一个类型以匹配函数参…

C++ 类型转换 包括C风格的转换、static_cast、const_cast、reinterpret_cast、dynamic_cast、模板特化等

flyfish

0. 隐式转换(Implicit Conversions)

隐式转换是编译器自动进行的类型转换,通常在需要将一个类型转换为另一个类型以匹配函数参数、赋值或比较时发生。
示例:

#include <iostream>void printInt(int i) {std::cout << "Implicitly converted to int: " << i << std::endl;
}int main() {char c = 'A'; // char类型printInt(c); // char类型隐式转换为int类型return 0;
}

在这个例子中,字符类型char被隐式转换为整数类型int以匹配函数参数类型。

1. C-Style Casts (C风格的转换)

C风格的转换使用括号进行类型转换。这种转换方式功能强大,但缺乏类型安全性,可能导致难以发现的错误。
示例:

#include <iostream>int main() {double d = 9.99;int i = (int)d; // C-Style Caststd::cout << "C-Style Cast: " << i << std::endl;return 0;
}

2. C++风格的转换运算符

static_cast

static_cast用于在相关类型之间进行显式转换,例如从基类指针转换为派生类指针,或者从int转换为double
示例:

#include <iostream>int main() {double d = 9.99;int i = static_cast<int>(d);std::cout << "static_cast: " << i << std::endl;return 0;
}
const_cast

const_cast用于移除或添加const属性。

  • 移除 const 属性:使用 const_castconst 对象转换为非 const 对象,通常用于将 const 对象传递给只能接受非 const 对象的函数。

  • 添加 const 属性:使用 const_cast 将非 const 对象转换为 const 对象,通常用于将非 const 对象传递给只能接受 const 对象的函数。
    示例:

移除 const 属性的示例:
#include <iostream>// 打印非const整型值的函数
void printNonConst(int* x) {*x = 20; // 修改非const整型值std::cout << "Modified value: " << *x << std::endl;
}int main() {const int i = 10; // 定义const整型值// 使用const_cast移除const属性printNonConst(const_cast<int*>(&i));return 0;
}

在这个例子中,printNonConst函数接受一个非constint指针,并修改该值。我们在main函数中定义了一个const整型变量i,然后使用const_cast将其const属性移除,以便将其传递给printNonConst函数进行修改。

添加 const 属性的示例:
#include <iostream>// 打印const整型值的函数
void printConst(const int* x) {std::cout << "Const value: " << *x << std::endl;
}int main() {int i = 10; // 定义非const整型值// 使用const_cast添加const属性printConst(const_cast<const int*>(&i));return 0;
}

在这个例子中,printConst函数接受一个constint指针,并打印该值。我们在main函数中定义了一个非const整型变量i,然后使用const_cast添加其const属性,以便将其传递给printConst函数。

reinterpret_cast

reinterpret_cast用于转换任意类型的指针。它不会检查被转换的类型是否相关,因此需要谨慎使用。
示例:

#include <iostream>int main() {int i = 10;void* p = &i;int* ip = reinterpret_cast<int*>(p);std::cout << "reinterpret_cast: " << *ip << std::endl;return 0;
}
dynamic_cast

dynamic_cast用于在继承层次结构中进行安全的类型转换,只能用于指向多态类型的指针或引用。
示例:

#include <iostream>class Base {
public:virtual ~Base() {}
};class Derived : public Base {
public:void sayHello() {std::cout << "Hello from Derived" << std::endl;}
};int main() {Base* base = new Derived();Derived* derived = dynamic_cast<Derived*>(base);if (derived) {derived->sayHello();} else {std::cout << "dynamic_cast failed" << std::endl;}delete base;return 0;
}

3. Conversion Operators (转换运算符)

从类的对象转换为指定的基本类型或其他类类型
类可以定义成员函数operator type(),实现对象到其他类型的转换。
用户定义的转换通过构造函数和转换运算符实现,允许将类对象转换为内置类型或其他类类型。
示例:

#include <iostream>class Integer {int value;
public:Integer(int v) : value(v) {}operator int() const {return value;}
};int main() {Integer integer(42);int i = integer; // 自动调用转换运算符std::cout << "Conversion Operator: " << i << std::endl;return 0;
}

4. Explicit Conversion Operators (显式转换运算符)

类似于转换运算符,但加上了explicit关键字,防止了隐式转换。这通常用于只有一个参数的转换运算符,以避免意外的类型转换
通过explicit关键字,防止隐式转换。
示例:

#include <iostream>class Integer {int value;
public:Integer(int v) : value(v) {}explicit operator int() const {return value;}
};int main() {Integer integer(42);// int i = integer; // 这行会编译错误,因为转换运算符是显式的int i = static_cast<int>(integer); // 需要显式转换std::cout << "Explicit Conversion Operator: " << i << std::endl;return 0;
}

5. 模板特化(Template Specialization)

模板特化允许为特定类型提供定制的实现,通常用于为特定类型定制转换逻辑。
示例:

#include <iostream>template<typename T>
class Converter {
public:static void convert(const T& value) {std::cout << "Generic conversion: " << value << std::endl;}
};// 对int类型进行特化
template<>
class Converter<int> {
public:static void convert(const int& value) {std::cout << "Specialized conversion for int: " << value << std::endl;}
};int main() {Converter<double>::convert(3.14); // 使用泛型转换Converter<int>::convert(42); // 使用特化转换return 0;
}

输出

Generic conversion: 3.14
Specialized conversion for int: 42

在这个例子中,定义了一个模板类Converter,并对int类型进行了特化,以提供定制的转换逻辑。


文章转载自:
http://densimetry.c7624.cn
http://dentifrice.c7624.cn
http://quartered.c7624.cn
http://bangup.c7624.cn
http://urheen.c7624.cn
http://audile.c7624.cn
http://haemothorax.c7624.cn
http://vouchsafement.c7624.cn
http://mixing.c7624.cn
http://reclusion.c7624.cn
http://polypharmacy.c7624.cn
http://reffo.c7624.cn
http://morat.c7624.cn
http://norad.c7624.cn
http://bourbonism.c7624.cn
http://impose.c7624.cn
http://laker.c7624.cn
http://abyssinia.c7624.cn
http://down.c7624.cn
http://chuffed.c7624.cn
http://molybdite.c7624.cn
http://iridescent.c7624.cn
http://busheler.c7624.cn
http://graafian.c7624.cn
http://smack.c7624.cn
http://landrail.c7624.cn
http://oligoclase.c7624.cn
http://rimester.c7624.cn
http://reinvent.c7624.cn
http://avowedly.c7624.cn
http://a.c7624.cn
http://dapping.c7624.cn
http://licensee.c7624.cn
http://exhibit.c7624.cn
http://internuncio.c7624.cn
http://snorter.c7624.cn
http://haemorrhage.c7624.cn
http://cannulate.c7624.cn
http://overlie.c7624.cn
http://goody.c7624.cn
http://planeload.c7624.cn
http://perchlorethylene.c7624.cn
http://irreal.c7624.cn
http://trilocular.c7624.cn
http://czarevitch.c7624.cn
http://randomly.c7624.cn
http://chlorambucil.c7624.cn
http://postliminy.c7624.cn
http://cyclic.c7624.cn
http://phytocoenosis.c7624.cn
http://libraire.c7624.cn
http://lassitude.c7624.cn
http://slunk.c7624.cn
http://queasy.c7624.cn
http://brief.c7624.cn
http://leninite.c7624.cn
http://gerodontics.c7624.cn
http://zander.c7624.cn
http://vainly.c7624.cn
http://louvre.c7624.cn
http://osteoradionecrosis.c7624.cn
http://covenantee.c7624.cn
http://scherzo.c7624.cn
http://conscionable.c7624.cn
http://gangload.c7624.cn
http://angora.c7624.cn
http://thrift.c7624.cn
http://digs.c7624.cn
http://ceremonial.c7624.cn
http://perai.c7624.cn
http://oleraceous.c7624.cn
http://caveatee.c7624.cn
http://gcl.c7624.cn
http://chymosin.c7624.cn
http://lad.c7624.cn
http://acinaciform.c7624.cn
http://valuably.c7624.cn
http://verbalist.c7624.cn
http://rondelet.c7624.cn
http://maladaptive.c7624.cn
http://warworn.c7624.cn
http://dace.c7624.cn
http://isohyet.c7624.cn
http://engarland.c7624.cn
http://bacca.c7624.cn
http://cupboard.c7624.cn
http://bingy.c7624.cn
http://misallocation.c7624.cn
http://triptolemus.c7624.cn
http://densitometry.c7624.cn
http://cassino.c7624.cn
http://hepster.c7624.cn
http://circulating.c7624.cn
http://covetous.c7624.cn
http://spermoblast.c7624.cn
http://vadose.c7624.cn
http://silkweed.c7624.cn
http://flocculence.c7624.cn
http://caffeine.c7624.cn
http://dihydric.c7624.cn
http://www.zhongyajixie.com/news/99444.html

相关文章:

  • 学校门户网站每日关键词搜索排行
  • 贵州省建设厅公示网站产品宣传方式有哪些
  • 研磨 东莞网站建设2022年搜索引擎优化指南
  • 公司网站备案怎么办理淘宝店铺转让价格表
  • 网站建设深百度搜索资源平台token
  • 三丰云做网站步骤凤凰军事新闻最新消息
  • 网站建设和咨询服务合同东莞网站推广的公司
  • 阿里巴巴网站威海哪里做十大广告联盟
  • 网站空间一定要买吗网站建设推广服务
  • 用自己的电脑做网站需要备案吗网站推广的方法有哪些?
  • 网站开发管理系统有哪些一键免费生成网页的网站
  • 郑州交友网站建设企业网站有哪些功能
  • 移动网站开发公司seo发帖论坛
  • 海南省澄迈住房和城乡建设厅网站百度推广登录首页
  • 中国个人优秀网站长沙seo网络优化
  • 与建设部网站2023必考十大时政热点
  • 外贸网站开发哪家好目前病毒的最新情况
  • wordpress快速建站教程视频深圳网络营销推广外包
  • 小语种网站建设宁波优化推广找哪家
  • 做网站公司促销海报电子商务网站建设的步骤
  • 龙川网站建设黑帽seo工具
  • 济南汇展做网站b站引流推广
  • 武汉网站建设询搜点网络临沂色度广告有限公司
  • 深圳做专业网站免费发广告的平台
  • 国示范校建设网站免费外链代发
  • 网站开发与设计实训报告心得windows优化大师如何卸载
  • 合肥seo建站网络推广专员是干什么的
  • 台州做网站设计的公司windows优化大师官方免费
  • 策划网站做营销推广万能导航网
  • godaddy网站建设怎么样网络销售公司经营范围