建设网站如何弄好几张网站背景沈阳建设工程信息网中项网

张小明 2026/1/19 19:32:06
建设网站如何弄好几张网站背景,沈阳建设工程信息网中项网,魔艺极速建站,免费发布租房信息网站当我们用“裸”指针进行类层次上的上下行转换时#xff0c;可以使用dynamic_cast。当然我们也可以使用static_cast#xff0c;只是dynamic_cast在进行下行转换的时候#xff08;即基类到派生类#xff09;具有类型检查功能#xff0c;而static_cast没有。因此存在安全问题…当我们用“裸”指针进行类层次上的上下行转换时可以使用dynamic_cast。当然我们也可以使用static_cast只是dynamic_cast在进行下行转换的时候即基类到派生类具有类型检查功能而static_cast没有。因此存在安全问题。当我们使用智能指针时如果需要进行类层次上的上下行转换时可以使用std::static_pointer_cast()、std::dynamic_pointer_cast、std::const_pointer_cast()和std::reinterpret_pointer_cast()。它们的功能和std::static_cast()、std::dynamic_cast、std::const_cast()和std::reinterpret_cast()类似只不过转换的是智能指针std::shared_ptr返回的也是std::shared_ptr类型。1、std::static_pointer_cast()当指针是智能指针时候向上转换用static_cast 则转换不了此时需要使用static_pointer_cast。2、std::dynamic_pointer_cast()当指针是智能指针时候向下转换用dynamic_cast 则转换不了此时需要使用dynamic_pointer_cast此处注意base基类需要至少有一个virtual成员函数即多态类型才能允许动态强制转换否则编译报错。3、std::const_pointer_cast()功能与std::const_cast()类似4、std::reinterpret_pointer_cast()功能与std::reinterpret_cast()类似Defined in header memorytemplate class T, class U std::shared_ptrT static_pointer_cast( const std::shared_ptrU r ) noexcept;(1) (since C11)template class T, class U std::shared_ptrT static_pointer_cast( std::shared_ptrU r ) noexcept;(2) (since C20)template class T, class U std::shared_ptrT dynamic_pointer_cast( const std::shared_ptrU r ) noexcept;(3) (since C11)template class T, class U std::shared_ptrT dynamic_pointer_cast( std::shared_ptrU r ) noexcept;(4) (since C20)template class T, class U std::shared_ptrT const_pointer_cast( const std::shared_ptrU r ) noexcept;(5) (since C11)template class T, class U std::shared_ptrT const_pointer_cast( std::shared_ptrU r ) noexcept;(6) (since C20)template class T, class U std::shared_ptrT reinterpret_pointer_cast( const std::shared_ptrU r ) noexcept;(7) (since C17)template class T, class U std::shared_ptrT reinterpret_pointer_cast( std::shared_ptrU r ) noexcept;(8) (since C20)基类和派生类的智能指针转换要使用std::dynamic_pointer_cast和std::static_pointer_cast。由于std::dynamic_pointer_cast和dynamic_cast原理一样std::static_pointer_cast和static_cast原理一样Creates a new instance of std::shared_ptr whose stored pointer is obtained from rs stored pointer using a cast expression.If r is empty, so is the new shared_ptr (but its stored pointer is not necessarily null). Otherwise, the new shared_ptr will share ownership with the initial value of r, except that it is empty if the dynamic_cast performed by dynamic_pointer_cast returns a null pointer.Let Y be typename std::shared_ptrT::element_type, then the resulting std::shared_ptrs stored pointer will be obtained by evaluating, respectively:1-2) static_castY*(r.get()).3-4) dynamic_castY*(r.get()) (If the result of the dynamic_cast is a null pointer value, the returned shared_ptr will be empty.)5-6) const_castY*(r.get()).7-8) reinterpret_castY*(r.get())The behavior of these functions is undefined unless the corresponding cast from U* to T* is well formed:1-2) The behavior is undefined unless static_castT*((U*)nullptr) is well formed.3-4) The behavior is undefined unless dynamic_castT*((U*)nullptr) is well formed.5-6) The behavior is undefined unless const_castT*((U*)nullptr) is well formed.7-8) The behavior is undefined unless reinterpret_castT*((U*)nullptr) is well formed.After calling the rvalue overloads (2,4,6,8), r is empty and r.get() nullptr, except that r is not modified for dynamic_pointer_cast (4) if the dynamic_cast fails.(since C20)Parametersr - The pointer to convertNotesThe expressions std::shared_ptrT(static_castT*(r.get())), std::shared_ptrT(dynamic_castT*(r.get())) and std::shared_ptrT(const_castT*(r.get())) might seem to have the same effect, but they all will likely result in undefined behavior, attempting to delete the same object twice!Possible implementation1、std::static_pointer_cast():template class T, class U std::shared_ptrT static_pointer_cast( const std::shared_ptrU r ) noexcept { auto p static_casttypename std::shared_ptrT::element_type*(r.get()); return std::shared_ptrT(r, p); }2、std::dynamic_pointer_cast()template class T, class U std::shared_ptrT dynamic_pointer_cast( const std::shared_ptrU r ) noexcept { if (auto p dynamic_casttypename std::shared_ptrT::element_type*(r.get())) { return std::shared_ptrT(r, p); } else { return std::shared_ptrT(); } }3、std::const_pointer_cast()template class T, class U std::shared_ptrT const_pointer_cast( const std::shared_ptrU r ) noexcept { auto p const_casttypename std::shared_ptrT::element_type*(r.get()); return std::shared_ptrT(r, p); }4、std::reinterpret_pointer_cast()template class T, class U std::shared_ptrT reinterpret_pointer_cast( const std::shared_ptrU r ) noexcept { auto p reinterpret_casttypename std::shared_ptrT::element_type*(r.get()); return std::shared_ptrT(r, p); }使用示例#include iostream #include memory struct Base { int a; virtual void f() const { std::cout I am base!\n;} virtual ~Base(){} }; struct Derived : Base { void f() const override { std::cout I am derived!\n; } ~Derived(){} }; int main(){ auto basePtr std::make_sharedBase(); std::cout Base pointer says: ; basePtr-f(); auto derivedPtr std::make_sharedDerived(); std::cout Derived pointer says: ; derivedPtr-f(); // static_pointer_cast to go up class hierarchy basePtr std::static_pointer_castBase(derivedPtr); std::cout Base pointer to derived says: ; basePtr-f(); // dynamic_pointer_cast to go down/across class hierarchy auto downcastedPtr std::dynamic_pointer_castDerived(basePtr); if(downcastedPtr) { std::cout Downcasted pointer says: ; downcastedPtr-f(); } // All pointers to derived share ownership std::cout Pointers to underlying derived: derivedPtr.use_count() \n; } Output: Base pointer says: I am base! Derived pointer says: I am derived! Base pointer to derived says: I am derived! Downcasted pointer says: I am derived! Pointers to underlying derived: 3 示例2 #include iostream // std::cout std::endl #include memory // std::shared_ptr std::dynamic_pointer_cast std::static_pointer_cast class base { public: virtual ~base(void) default; }; class derived : public base { }; class test : public base { }; int main(void) { std::cout std::boolalpha; // 两个不同的派生类对象 auto derivedobj std::make_sharedderived(); auto testobj std::make_sharedtest(); // 隐式转换 derived-base std::shared_ptrbase pointer1 derivedobj; // static_pointer_cast derived-base auto pointer2 std::static_pointer_castbase(derivedobj); // dynamic_pointer_cast base-derived auto pointer3 std::dynamic_pointer_castderived(pointer1); std::cout (pointer3 nullptr) std::endl; // dynamic_pointer_cast base-derived auto pointer4 std::dynamic_pointer_casttest(pointer1); std::cout (pointer4 nullptr) std::endl; return 0; }输出结果falsetruestd::reinterpret_pointer_cast()和std::const_pointer_cast()示例代码#include memory #include cassert #include cstdint int main() { std::shared_ptrint foo; std::shared_ptrconst int bar; foo std::make_sharedint(10); bar std::const_pointer_castconst int(foo); std::cout *bar: *bar std::endl; *foo 20; std::cout *bar: *bar std::endl; std::shared_ptrstd::int8_t[] p(new std::int8_t[4]{1, 1, 1, 1}); std::shared_ptrstd::int32_t[] q std::reinterpret_pointer_caststd::int32_t[](p); std::int32_t r q[0]; std::int32_t x (1 8) | (1 16) | (1 24) | 1; assert(r x); return 0; }输出*bar: 10*bar: 20
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

湛江网站制作江网站制作网站的建设公司哪家好

DeepEP终极指南:5大实用技巧助你实现Ampere GPU专家并行通信性能调优 【免费下载链接】DeepEP DeepEP: an efficient expert-parallel communication library 项目地址: https://gitcode.com/GitHub_Trending/de/DeepEP 你是否正为Ampere GPU集群上的专家并行…

张小明 2026/1/17 16:10:15 网站建设

专业的建网站公司地址域名备案查询网站备案信息查询

Qwen3-VL构建智能镜像系统:让开发者无缝访问Stack Overflow 在今天这个信息爆炸的时代,程序员每天都在与时间赛跑。一个简单的语法错误可能卡住半天,而最高效的解决方案往往就藏在Stack Overflow的某个角落——前提是,你能顺利打开…

张小明 2026/1/17 16:10:16 网站建设

一元云够网站建设企业策划咨询公司

👉 这是一个或许对你有用的社群🐱 一对一交流/面试小册/简历优化/求职解惑,欢迎加入「芋道快速开发平台」知识星球。下面是星球提供的部分资料: 《项目实战(视频)》:从书中学,往事上…

张小明 2026/1/17 16:10:17 网站建设

网站备案成功后怎么建设网站建设思维导图的要求

作为Python零基础小白,我对“实战项目”的最初认知,就是“跟着视频抄代码”。直到上手《Python实现自动抢12306高铁票》视频实操,才发现问题所在:视频里能一键运行的代码,我改个出发地就报错;看似简单的反爬…

张小明 2026/1/17 16:10:20 网站建设

清河网站建设设计哪里可以学做资料员的网站

终极HTML5游戏音效制作指南:jsfxr让音效创作变得超简单 【免费下载链接】jsfxr Dead easy game sound effects generator. A port of sfxr to HTML5. 项目地址: https://gitcode.com/gh_mirrors/js/jsfxr jsfxr是一个基于HTML5的开源游戏音效生成器&#xff…

张小明 2026/1/17 16:10:20 网站建设

徐州网站建设青州陈酿社区推广

Linux 文件操作与管理技巧 1. 文件类型统计枚举 在 Linux 系统中,文件类型丰富多样。编写一个脚本,遍历目录及其子目录下的所有文件,并输出每种文件类型及其数量的统计报告,是一项有趣且实用的任务。 1.1 准备工作 在 UNIX/Linux 系统中,文件类型并非像 Windows 那样由…

张小明 2026/1/17 16:10:21 网站建设