日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C++強(qiáng)制轉(zhuǎn)換與智能指針示例詳解_C 語言

作者:lpf_wei ? 更新時間: 2022-12-21 編程語言

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

#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 將常量對象,強(qiáng)轉(zhuǎn)為非常量對象,一達(dá)到修改常量指針的值得目的。

const相關(guān)的強(qiáng)轉(zhuǎn)都可以使用這個來強(qiáng)轉(zhuǎn)

2.C++強(qiáng)制轉(zhuǎn)換static_cast(指針相關(guān)的)

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++強(qiáng)制轉(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)換的時候,在運(yùn)行期 由于fuClass 是new 父類的,就不能轉(zhuǎn)換子類(new 的是誰就能轉(zhuǎn)誰)
  • 動態(tài)轉(zhuǎn)換是有返回值, null 轉(zhuǎn)換失敗

4.C++強(qiáng)制類型轉(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底層廣泛應(yīng)用
  • 主要是可以將對象轉(zhuǎn)換long,然后保存long類型,等到使用的時候?qū)ong類型再轉(zhuǎn)回對象

5.C++智能指針之shared_ptr

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

#include <iostream>
#include <memory>
using namespace std;
class Person2; // 先聲明 Person2 讓我們的Person1 直接找到
class Person1 {
public:
    shared_ptr<Person2> person2; // Person2智能指針  shared_ptr 引用計(jì)數(shù)+1
    ~Person1() {
        cout << "Person1 析構(gòu)函數(shù)" << endl;
    }
};
class Person2 {
public:
    shared_ptr<Person1> person1;  // Person1智能指針  shared_ptr 引用計(jì)數(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的引用計(jì)數(shù)是:" << sharedPtr1.use_count() << endl;
    cout << "前 sharedPtr2的引用計(jì)數(shù)是:" << sharedPtr2.use_count() << endl;
//    // 給Person2智能指針賦值   循環(huán)依賴 智能指針就失去了自動釋放內(nèi)存的功能了
//    person1->person2 = sharedPtr2;
//    // 給Person1智能指針賦值
//    person2->person1 = sharedPtr1;
//
//
//    cout << "后 sharedPtr1的引用計(jì)數(shù)是:" << sharedPtr1.use_count() << endl;
//    cout << "后 sharedPtr2的引用計(jì)數(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智能指針  沒有引用計(jì)數(shù) 無法+1
    ~Person1() {
        cout << "Person1 析構(gòu)函數(shù)" << endl;
    }
};
class Person2 {
public:
    weak_ptr<Person1> person1;  // Person1智能指針  沒有引用計(jì)數(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的引用計(jì)數(shù)是:" << sharedPtr1.use_count() << endl;
    cout << "前 sharedPtr2的引用計(jì)數(shù)是:" << sharedPtr2.use_count() << endl;
    // 給Person2智能指針賦值
    person1->person2 = sharedPtr2;
    // 給Person1智能指針賦值
    person2->person1 = sharedPtr1;
    cout << "后 sharedPtr1的引用計(jì)數(shù)是:" << sharedPtr1.use_count() << endl;
    cout << "后 sharedPtr2的引用計(jì)數(shù)是:" << sharedPtr2.use_count() << endl;
    return 0;
} // 減1 = 0 釋放對象

7.C++智能指針unique_ptr

實(shí)現(xiàn)獨(dú)占式引用,保證同一時間只有一個智能指針指向內(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);
    // 嚴(yán)格禁止
//     std::unique_ptr<Person> uniquePtr2 = uniquePtr1;  unique不允許,因?yàn)槭仟?dú)占的
    // shared_ptr 是可以的,會造成隱患問題
    return 0;
}

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

#include <iostream>
using namespace std;
template<typename T>
class Ptr{
private:
    T * object;         // 用于智能指針指向管理的對象  Person Student
    int * count;        //引用計(jì)數(shù)
public:
    Ptr(){
        count=new int(1);  // new 的對象 必須 *指針  
        object=0;  // 因?yàn)槟銢]有給他對象 智能賦值為0
    }
    Ptr(T *t):object(t){
//        只有你存入對象,那么引用計(jì)數(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
    }
    // 自定義 =號運(yùn)算符重載
    Ptr<T> & operator = (const Ptr<T> & p){
        cout << "=號運(yùn)算符重載" << endl;
        ++(*p.count);

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

原文鏈接:https://blog.csdn.net/u014078003/article/details/126456927

欄目分類
最近更新