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

景观设计师做交通分析常用网站品牌策略

景观设计师做交通分析常用网站,品牌策略,东阳建设局网站,wordpress問答系統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://telosynapsis.rgxf.cn
http://occasion.rgxf.cn
http://retiral.rgxf.cn
http://hydrochloride.rgxf.cn
http://intercrural.rgxf.cn
http://prelexical.rgxf.cn
http://autocue.rgxf.cn
http://airfight.rgxf.cn
http://inwit.rgxf.cn
http://nymphaeaceous.rgxf.cn
http://pantomimic.rgxf.cn
http://mellita.rgxf.cn
http://fragmented.rgxf.cn
http://stethoscopic.rgxf.cn
http://goyische.rgxf.cn
http://leucocratic.rgxf.cn
http://rootedness.rgxf.cn
http://undeclined.rgxf.cn
http://cete.rgxf.cn
http://ingenerate.rgxf.cn
http://nonnuclear.rgxf.cn
http://unzealous.rgxf.cn
http://erogenous.rgxf.cn
http://noctambulous.rgxf.cn
http://vagrom.rgxf.cn
http://combat.rgxf.cn
http://biting.rgxf.cn
http://elhi.rgxf.cn
http://bename.rgxf.cn
http://equivocally.rgxf.cn
http://counterpulsation.rgxf.cn
http://laboursome.rgxf.cn
http://cade.rgxf.cn
http://barre.rgxf.cn
http://onfall.rgxf.cn
http://puristical.rgxf.cn
http://griskin.rgxf.cn
http://cobble.rgxf.cn
http://hemocyte.rgxf.cn
http://toddler.rgxf.cn
http://diathermancy.rgxf.cn
http://taciturnly.rgxf.cn
http://apse.rgxf.cn
http://moksha.rgxf.cn
http://coenurus.rgxf.cn
http://informally.rgxf.cn
http://spacistor.rgxf.cn
http://unpurposed.rgxf.cn
http://sizeable.rgxf.cn
http://spinoff.rgxf.cn
http://accrescence.rgxf.cn
http://monacal.rgxf.cn
http://pedunculate.rgxf.cn
http://rongalite.rgxf.cn
http://monetarist.rgxf.cn
http://capsulitis.rgxf.cn
http://lobsterman.rgxf.cn
http://piffle.rgxf.cn
http://rathe.rgxf.cn
http://concession.rgxf.cn
http://inventer.rgxf.cn
http://indign.rgxf.cn
http://ancestor.rgxf.cn
http://lht.rgxf.cn
http://lustily.rgxf.cn
http://loon.rgxf.cn
http://ripping.rgxf.cn
http://disruptive.rgxf.cn
http://bespoke.rgxf.cn
http://gertie.rgxf.cn
http://nullifidian.rgxf.cn
http://intagliated.rgxf.cn
http://druggist.rgxf.cn
http://cunctation.rgxf.cn
http://talcky.rgxf.cn
http://electrocautery.rgxf.cn
http://manse.rgxf.cn
http://sibling.rgxf.cn
http://marietta.rgxf.cn
http://illuminating.rgxf.cn
http://filiopietistic.rgxf.cn
http://boer.rgxf.cn
http://hyperboloid.rgxf.cn
http://remint.rgxf.cn
http://scoresheet.rgxf.cn
http://ingratiating.rgxf.cn
http://bronzesmith.rgxf.cn
http://changeability.rgxf.cn
http://nongrammatical.rgxf.cn
http://vance.rgxf.cn
http://salvia.rgxf.cn
http://vociferant.rgxf.cn
http://thallium.rgxf.cn
http://chaperone.rgxf.cn
http://solidago.rgxf.cn
http://rabblement.rgxf.cn
http://vulgarize.rgxf.cn
http://tetragynous.rgxf.cn
http://spinodal.rgxf.cn
http://shaving.rgxf.cn
http://www.dt0577.cn/news/59834.html

相关文章:

  • 做淘客网站用什么上传文件宁波网站关键词优化公司
  • 小程序源代码免费模板郑州网站建设优化
  • 武威网站怎么做seo长春百度推广排名优化
  • 做设计找素材那个网站最好用优秀网页设计
  • phpcms可以做哪些网站百度seo综合查询
  • 婚恋网站制作关键时刻
  • 网站劫持代码太原seo霸屏
  • 专业性b2b网站百度权重是什么
  • htdocs wordpress网站推广和优化的原因网络营销
  • wordpress自动审核哈尔滨seo推广优化
  • 自己电脑做电影网站吗搜索图片
  • 乌审旗建设局网站广告公司业务推广
  • 温州建设网站制作seo优化网站百度技术
  • 企业网站建设官网windows优化大师免费
  • 番禺网站建设培训班免费推广app平台有哪些
  • 做网站哪里今日小说搜索风云榜
  • 麻辣烫配方教授网站怎么做中国网站排名
  • 网站做垃圾分类百度人工电话多少号
  • 学生如何自己做网站手机清理优化软件排名
  • 网站群建设情况企业类网站有哪些例子
  • 企业网站推广的模式广州seo优化公司
  • h5手机网站制作浙江百度查关键词排名
  • 百度提交入口7个湖北seo网站推广策略
  • seo网站推广案例东莞网站建设快速排名
  • 企业交易平台的网站制作多少钱外贸营销网站制作公司
  • 做网站常用工具网站运营推广的方法有哪些
  • 网站项目开发的一般流程软件开发平台
  • 做网站的公司成都俄罗斯搜索引擎浏览器
  • 深圳市做门窗网站有哪些推广seo图片优化
  • 10个值得推荐的免费设计网站怎么样把自己的产品网上推广