最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++強制轉(zhuǎn)換與智能指針示例詳解

 更新時間:2022年11月22日 09:16:57   作者:lpf_wei  
這篇文章主要介紹了C++強制轉(zhuǎn)換與智能指針示例,智能指針(Smart Pointer)是一種抽象的數(shù)據(jù)類型。在程序設計中,它通常是經(jīng)由類模板來實現(xiàn),借由模板來達成泛型,借由類別的析構(gòu)函數(shù)來達成自動釋放指針所指向的存儲器或?qū)ο?/div>

1.C++強制轉(zhuǎn)換之const_cast(cosnt常量相關的)

#include <iostream>
using namespace std;
class Person {
public:
    string name = "小舞";
};
int main() {
    const Person * p1 = new Person();
    // p1->name = "Derry"; // 報錯:常量指針,不寫修改值
    Person * p2 = const_cast<Person *>(p1); // 轉(zhuǎn)成 非常量指針
    p2->name = "唐三";
    cout << p1->name.c_str() << endl;
    return 0;
}

通過const_cast 將常量對象,強轉(zhuǎn)為非常量對象,一達到修改常量指針的值得目的。

const相關的強轉(zhuǎn)都可以使用這個來強轉(zhuǎn)

2.C++強制轉(zhuǎn)換static_cast(指針相關的)

class FuClass {
public:
    void show() {
        cout << "fu show" << endl;
    }
};
class ZiClass  : public FuClass {
public:
    void show() {
        cout << "zi show" << endl;
    }
};
int main() {
    FuClass * fuClass = new FuClass;
    // fuClass->show();
    ZiClass * ziClass = static_cast<ZiClass *>(fuClass);
    ziClass->show();
    delete fuClass; 
    return 0;
}
  • static_cast(編譯期) 看左邊 ZiClass * 左邊是什么類型就調(diào)用什么類型的方法
  • 回收規(guī)則:一定是誰new了,我就delete誰

3.C++強制轉(zhuǎn)換dynamic_cast動態(tài)轉(zhuǎn)換

#include <iostream>
using namespace std;
class FuClass {
public:
    // 動態(tài)轉(zhuǎn)換必須讓父類成為虛函數(shù)
    virtual void show() {
        cout << "fu show" << endl;
    }
};
class ZiClass  : public FuClass {
public:
    void show() {
        cout << "zi show" << endl;
    }
};
int main() {
    // FuClass * fuClass = new FuClass(); // 失敗
    FuClass * fuClass = new ZiClass; //子類 成功
    ZiClass * ziClass = dynamic_cast<ZiClass *>(fuClass);
    if (ziClass) { // ziClass != null
        cout << "轉(zhuǎn)換成功 " ;
        ziClass->show();
    } else {
        cout << "轉(zhuǎn)換失敗" << endl ;
    }
    return 0;
}
  • 動態(tài)類型轉(zhuǎn)換的時候,在運行期 由于fuClass 是new 父類的,就不能轉(zhuǎn)換子類(new 的是誰就能轉(zhuǎn)誰)
  • 動態(tài)轉(zhuǎn)換是有返回值, null 轉(zhuǎn)換失敗

4.C++強制類型轉(zhuǎn)換reinterpret_cast

#include <iostream>
using namespace std;
class Handler {
public:
    void show() {
        cout << "handler" << endl;
    }
};
int main() {
    Handler * handler = new Handler();
    long handlerValue = reinterpret_cast<long>(handler); // 把對象變成數(shù)值
    // 通過數(shù)值 變成對象
    Handler * handler2 = reinterpret_cast<Handler *>(handlerValue);
    handler2->show();
    printf("handler:%p\n", handler);
    printf("handler2:%p\n", handler2);
}
  • reinterpret_cast 用于引用句柄,在NDK以及Android底層廣泛應用
  • 主要是可以將對象轉(zhuǎn)換long,然后保存long類型,等到使用的時候?qū)ong類型再轉(zhuǎn)回對象

5.C++智能指針之shared_ptr

智能指針作用是為了自動釋放堆對象內(nèi)存,真實項目中由于代碼量很大,可能會忘記對堆內(nèi)存的釋放,為了解決這個問題,引入的智能指針。

#include <iostream>
#include <memory>
using namespace std;
class Person2; // 先聲明 Person2 讓我們的Person1 直接找到
class Person1 {
public:
    shared_ptr<Person2> person2; // Person2智能指針  shared_ptr 引用計數(shù)+1
    ~Person1() {
        cout << "Person1 析構(gòu)函數(shù)" << endl;
    }
};
class Person2 {
public:
    shared_ptr<Person1> person1;  // Person1智能指針  shared_ptr 引用計數(shù)+1
    ~Person2() {
        cout << "Person2 析構(gòu)函數(shù)" << endl;
    }
};
int main(){
    Person1 * person1 = new Person1(); // 堆區(qū)開辟
    Person2 * person2 = new Person2(); // 堆區(qū)開辟
    shared_ptr<Person1> sharedPtr1(person1); // +1 = 1
    shared_ptr<Person2> sharedPtr2(person2); // +1 = 1
    cout << "前 sharedPtr1的引用計數(shù)是:" << sharedPtr1.use_count() << endl;
    cout << "前 sharedPtr2的引用計數(shù)是:" << sharedPtr2.use_count() << endl;
//    // 給Person2智能指針賦值   循環(huán)依賴 智能指針就失去了自動釋放內(nèi)存的功能了
//    person1->person2 = sharedPtr2;
//    // 給Person1智能指針賦值
//    person2->person1 = sharedPtr1;
//
//
//    cout << "后 sharedPtr1的引用計數(shù)是:" << sharedPtr1.use_count() << endl;
//    cout << "后 sharedPtr2的引用計數(shù)是:" << sharedPtr2.use_count() << endl;
    return 0;
}

雖然使用shared_ptr能夠非常方便的為我們自動釋放對象,但是還是會出現(xiàn)一些問題。最典型的就是循環(huán)引用問題。

6.C++智能指針之weak_ptr

weak_ptr是為配合shared_ptr而引入的一種智能指針。主要用于觀測資源的引用情況。它的構(gòu)造和析構(gòu)不會引起引用記數(shù)的增加或減少。沒有重載*和->但可以使用lock獲得一個可用的shared_ptr對象。

配合shared_ptr解決循環(huán)引用問題

#include <iostream>
#include <memory> // 智能指針的頭文件引入
using namespace std;
class Person2; // 先聲明 Person2 讓我們的Person1 直接找到
class Person1 {
public:
    weak_ptr<Person2> person2; // Person2智能指針  沒有引用計數(shù) 無法+1
    ~Person1() {
        cout << "Person1 析構(gòu)函數(shù)" << endl;
    }
};
class Person2 {
public:
    weak_ptr<Person1> person1;  // Person1智能指針  沒有引用計數(shù) 無法+1
    ~Person2() {
        cout << "Person2 析構(gòu)函數(shù)" << endl;
    }
};
int main() {
    Person1 * person1 = new Person1(); // 堆區(qū)開辟
    Person2 * person2 = new Person2(); // 堆區(qū)開辟
    shared_ptr<Person1> sharedPtr1(person1); // +1 = 1
    shared_ptr<Person2> sharedPtr2(person2); // +1 = 1
    cout << "前 sharedPtr1的引用計數(shù)是:" << sharedPtr1.use_count() << endl;
    cout << "前 sharedPtr2的引用計數(shù)是:" << sharedPtr2.use_count() << endl;
    // 給Person2智能指針賦值
    person1->person2 = sharedPtr2;
    // 給Person1智能指針賦值
    person2->person1 = sharedPtr1;
    cout << "后 sharedPtr1的引用計數(shù)是:" << sharedPtr1.use_count() << endl;
    cout << "后 sharedPtr2的引用計數(shù)是:" << sharedPtr2.use_count() << endl;
    return 0;
} // 減1 = 0 釋放對象

7.C++智能指針unique_ptr

實現(xiàn)獨占式引用,保證同一時間只有一個智能指針指向內(nèi)部對象。

#include <iostream>
#include <memory> // 智能指針的頭文件引入
using namespace std;
class Person {
public:
    ~Person(){
        cout<<"析構(gòu)函數(shù)"<<endl;
    }
};
int main() {
    Person * person1 = new Person();
    Person * person2 = new Person();
    unique_ptr<Person> uniquePtr1(person1);
    // 嚴格禁止
//     std::unique_ptr<Person> uniquePtr2 = uniquePtr1;  unique不允許,因為是獨占的
    // shared_ptr 是可以的,會造成隱患問題
    return 0;
}

8.自定義實現(xiàn)類似shared_ptr智能指針

#include <iostream>
using namespace std;
template<typename T>
class Ptr{
private:
    T * object;         // 用于智能指針指向管理的對象  Person Student
    int * count;        //引用計數(shù)
public:
    Ptr(){
        count=new int(1);  // new 的對象 必須 *指針  
        object=0;  // 因為你沒有給他對象 智能賦值為0
    }
    Ptr(T *t):object(t){
//        只有你存入對象,那么引用計數(shù)為1,這個是很合理的
        count=new int(1);
    }
    ~Ptr(){
        if (--(*count)==0){
            if (object){
                delete object;
            }
            delete count;
            object=0;
            count=0;
        }
    }
    //拷貝構(gòu)造函數(shù)
    Ptr(const Ptr<T> & p){
        cout << "拷貝構(gòu)造函數(shù)" << endl;
        ++(*p.count);
        object = p.object;
        count = p.count; // 最終是不是 p.count==2 給 count==2
    }
    // 自定義 =號運算符重載
    Ptr<T> & operator = (const Ptr<T> & p){
        cout << "=號運算符重載" << endl;
        ++(*p.count);

        if (--(*count) == 0) {
            if (object) {
                delete object;
            }
            delete count;
        }
        object = p.object;
        count = p.count;
        return *this; // 運算符重載的返回
    }
    // 返回引用計數(shù)的數(shù)值
    int use_count() {
        return *this->count;
    }
};
int main(){
    //自定義智能指針的應用
    Student *student1 = new Student();
    Student *student2 = new Student();
    Ptr<Student> sharedPtr1(student1);
    Ptr<Student> sharedPtr2(student2);
    return 0;
}

到此這篇關于C++強制轉(zhuǎn)換與智能指針示例 詳解的文章就介紹到這了,更多相關C++強制轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C++運算符重載實例代碼詳解(調(diào)試環(huán)境 Visual Studio 2019)

    C++運算符重載實例代碼詳解(調(diào)試環(huán)境 Visual Studio 2019)

    這篇文章主要介紹了C++運算符重載實例(調(diào)試環(huán)境 Visual Studio 2019),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Java?C++題解leetcode1598文件夾操作日志搜集器

    Java?C++題解leetcode1598文件夾操作日志搜集器

    這篇文章主要為大家介紹了Java?C++題解leetcode1598文件夾操作日志搜集器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • C++中inline函數(shù)詳解

    C++中inline函數(shù)詳解

    inline函數(shù)的定義:在函數(shù)聲明或定義中函數(shù)返回類型前加上關鍵字inline,即可以把函數(shù)指定為內(nèi)聯(lián)函數(shù)。inline函數(shù)對編譯器而言必須是可見的,以便它能夠在調(diào)用點展開該函數(shù)。
    2015-07-07
  • C++模板全方位深入解讀

    C++模板全方位深入解讀

    人們需要編寫多個形式和功能都相似的函數(shù),因此有了函數(shù)模板來減少重復勞動;人們也需要編寫多個形式和功能都相似的類,于是?C++?引人了類模板的概念,編譯器從類模板可以自動生成多個類,避免了程序員的重復勞動
    2022-06-06
  • C++實現(xiàn)數(shù)組的排序/插入重新排序/以及逆置操作詳解

    C++實現(xiàn)數(shù)組的排序/插入重新排序/以及逆置操作詳解

    將新的數(shù)字與已經(jīng)排序好的數(shù)組中的數(shù)字一一比較,直到找到插入點,然后將插入點以后的數(shù)字都向后移動一個單位(a[i+1]=a[i]),然后將數(shù)據(jù)插入即可
    2013-10-10
  • 使用Cline+deepseek實現(xiàn)VsCode自動化編程

    使用Cline+deepseek實現(xiàn)VsCode自動化編程

    Cline是一個免費且強大的VSCode插件,可以接入多種大模型API進行對話式編程,本文主要介紹了使用Cline+deepseek實現(xiàn)VsCode自動化編程,具有一定的參考價值,感興趣的可以了解一下
    2025-01-01
  • C語言實現(xiàn)簡易訂餐系統(tǒng)

    C語言實現(xiàn)簡易訂餐系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡易訂餐系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C#桌面應用開發(fā)實現(xiàn)番茄定時器

    C#桌面應用開發(fā)實現(xiàn)番茄定時器

    本文主要介紹了C#桌面應用開發(fā)實現(xiàn)番茄定時器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-07-07
  • C語言數(shù)據(jù)結(jié)構(gòu)系列隊列篇

    C語言數(shù)據(jù)結(jié)構(gòu)系列隊列篇

    本章我們將學習 "隊列" ,首先介紹隊列的概念和結(jié)構(gòu),然后我們將著重講解棧的實現(xiàn)。我們從零開始寫隊列的接口,并從零開始步步解讀。本章將繼續(xù)鞏固畫思路草圖的能力,只要思路草圖畫好了,就可以很輕松地將其轉(zhuǎn)換成代碼
    2022-02-02
  • C++中的std::format?如何實現(xiàn)編譯期格式檢查

    C++中的std::format?如何實現(xiàn)編譯期格式檢查

    C++?20?的?std::format?是一個很神奇、很實用的工具,最神奇的地方在于它能在編譯期檢查字符串的格式是否正確,而且不需要什么特殊的使用方法,只需要像使用普通函數(shù)那樣傳參即可,這篇文章主要介紹了std::format?如何實現(xiàn)編譯期格式檢查,需要的朋友可以參考下
    2024-04-04

最新評論

淮安市| 乳源| 志丹县| 新建县| 巴南区| 常宁市| 龙岩市| 南京市| 通江县| 乐昌市| 南平市| 垣曲县| 翁源县| 凤台县| 大洼县| 汶川县| 祁门县| 手游| 新密市| 洛阳市| 四平市| 蒙山县| 吉首市| 昌邑市| 兰州市| 梅河口市| 田阳县| 青铜峡市| 眉山市| 报价| 敦煌市| 永城市| 苍溪县| 湖南省| 云南省| 陇川县| 岚皋县| 墨脱县| 谢通门县| 嘉黎县| 辉县市|