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

C++探索構(gòu)造函數(shù)私有化會(huì)產(chǎn)生什么結(jié)果

 更新時(shí)間:2022年05月18日 11:13:40   作者:OceanStar的學(xué)習(xí)筆記  
C++的構(gòu)造函數(shù)的作?:初始化類對象的數(shù)據(jù)成員。即類的對象被創(chuàng)建的時(shí)候,編譯系統(tǒng)對該對象分配內(nèi)存空間,并?動(dòng)調(diào)?構(gòu)造函數(shù),完成類成員的初始化。構(gòu)造函數(shù)的特點(diǎn):以類名作為函數(shù)名,?返回類型

提問:假設(shè)只有一個(gè)構(gòu)造方法,如果將之私有化會(huì)有什么后果

  • 對于當(dāng)前類,它是無法實(shí)例化的
  • 對于它的子類,子類也是無法實(shí)例化的

構(gòu)造函數(shù)與是否能夠?qū)嵗嘘P(guān)

對于單個(gè)類

正常情況下

#include <iostream>
using namespace std;
class EventDispatcher {
public:
    void test_printf(){
        std::cout << "test_printf --\r\n";
    }
    EventDispatcher() = default;
};
int main(int argc,char *argv[]){
    EventDispatcher noticeCenter1;
    EventDispatcher *noticeCenter2 = new EventDispatcher;
    noticeCenter1.test_printf();
    noticeCenter2->test_printf();
}

構(gòu)造函數(shù)私有化

#include <iostream>
using namespace std;
class EventDispatcher {
public:
    void test_printf(){
        std::cout << "test_printf --\r\n";
    }
private:
    EventDispatcher() = default;
};
int main(int argc,char *argv[]){
    EventDispatcher noticeCenter1;
    EventDispatcher *noticeCenter2 = new EventDispatcher;
    noticeCenter1.test_printf();
    noticeCenter2->test_printf();
}

編譯通不過,因?yàn)闊o論是在棧還是堆上,都無法調(diào)用構(gòu)造函數(shù)來生成對象

私有化與繼承

正常情況下

#include <iostream>
using namespace std;
class EventDispatcher {
public:
    void test_printf(){
        std::cout << "test_printf --\r\n";
    }
    EventDispatcher() = default;
};
class NoticeCenter : public  EventDispatcher{
public:
    void test_Center(){
        std::cout << "test_Center --\r\n";
    }
};
int main(int argc,char *argv[]){
    NoticeCenter noticeCenter1;
    NoticeCenter *noticeCenter2 = new NoticeCenter;
    noticeCenter1.test_printf();
    noticeCenter2->test_printf();
    noticeCenter1.test_Center();
    noticeCenter2->test_Center();
}

2. 父類構(gòu)造函數(shù)私有化,而且子類沒有提供public的構(gòu)造函數(shù)----》 子類的構(gòu)造函數(shù)也是私有化的

#include <iostream>
using namespace std;
class EventDispatcher {
public:
    void test_printf(){
        std::cout << "test_printf --\r\n";
    }
private:
    EventDispatcher() = default;
};
class NoticeCenter : public  EventDispatcher{
public:
    void test_Center(){
        std::cout << "test_Center --\r\n";
    }
};
int main(int argc,char *argv[]){
    NoticeCenter noticeCenter1;
    NoticeCenter *noticeCenter2 = new NoticeCenter;
    noticeCenter1.test_printf();
    noticeCenter2->test_printf();
    noticeCenter1.test_Center();
    noticeCenter2->test_Center();
}

父類構(gòu)造函數(shù)私有化,而且子類提供public的構(gòu)造函數(shù)----》編譯還是不能通過

#include <iostream>
using namespace std;
class EventDispatcher {
public:
    void test_printf(){
        std::cout << "test_printf --\r\n";
    }
private:
    EventDispatcher() = default;
};
class NoticeCenter : public  EventDispatcher{
public:
    void test_Center(){
        std::cout << "test_Center --\r\n";
    }
public:
    NoticeCenter() = default;  //沒有作用
 	//此時(shí)子類無法提供除了默認(rèn)構(gòu)造函數(shù)之外的函數(shù),比如 NoticeCenter(int a)
};
int main(int argc,char *argv[]){
    NoticeCenter noticeCenter1;
    NoticeCenter *noticeCenter2 = new NoticeCenter;
    noticeCenter1.test_printf();
    noticeCenter2->test_printf();
    noticeCenter1.test_Center();
    noticeCenter2->test_Center();
}

結(jié)論:只要繼承了一個(gè)無法實(shí)例化的父類,不管子類怎么折騰,都無法實(shí)例化。 這也是noncopyable類的由來

成員變量與私有化

正常情況下

#include <iostream>
using namespace std;
class EventDispatcher {
public:
    void test_printf(){
        std::cout << "test_printf --\r\n";
    }
    EventDispatcher() = default;
};
class NoticeCenter {
public:
    void test_Center(){
        a.test_printf();
        std::cout << "test_Center --\r\n";
    }
    EventDispatcher a;
};
int main(int argc,char *argv[]){
    NoticeCenter noticeCenter1;
    NoticeCenter *noticeCenter2 = new NoticeCenter;
    noticeCenter1.test_Center();
    noticeCenter2->test_Center();
}

2. 如果當(dāng)前類的某個(gè)成員變量是無法實(shí)例化的,那么當(dāng)前類也無法實(shí)例化(正常,某個(gè)組件無法實(shí)例化,那么整個(gè)構(gòu)建就會(huì)出問題)

#include <iostream>
using namespace std;
class EventDispatcher {
public:
    void test_printf(){
        std::cout << "test_printf --\r\n";
    }
private:
    EventDispatcher() = default;
};
class NoticeCenter {
public:
    void test_Center(){
        std::cout << "test_Center --\r\n";
        a.test_printf();
    }
    EventDispatcher a;
};
int main(int argc,char *argv[]){
    NoticeCenter noticeCenter1;
    NoticeCenter *noticeCenter2 = new NoticeCenter;
    noticeCenter1.test_Center();
    noticeCenter2->test_Center();
}

解決方法:友元類可以訪問某個(gè)類的私有成員,所以將令構(gòu)件為某個(gè)組件的友元類,這樣構(gòu)件就可以去訪問組件私有的構(gòu)造函數(shù),將之構(gòu)造出來了

#include <iostream>
using namespace std;
class EventDispatcher {
    friend class NoticeCenter ;
public:
    void test_printf(){
        std::cout << "test_printf --\r\n";
    }
private:
    EventDispatcher() = default;
};
class NoticeCenter {
public:
    void test_Center(){
        std::cout << "test_Center --\r\n";
        a.test_printf();
    }
    EventDispatcher a;
};
int main(int argc,char *argv[]){
    NoticeCenter noticeCenter1;
    NoticeCenter *noticeCenter2 = new NoticeCenter;
    noticeCenter1.test_Center();
    noticeCenter2->test_Center();
}

到此這篇關(guān)于C語言探索構(gòu)造函數(shù)私有化會(huì)產(chǎn)生什么結(jié)果的文章就介紹到這了,更多相關(guān)C語言構(gòu)造函數(shù)私有化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++ main函數(shù)的幾點(diǎn)細(xì)節(jié)

    C++ main函數(shù)的幾點(diǎn)細(xì)節(jié)

    這篇文章主要介紹了C++ main函數(shù)的幾點(diǎn)細(xì)節(jié),幫助大家更好的理解和學(xué)習(xí)C++,感興趣的朋友可以了解下
    2020-08-08
  • QT基于TCP實(shí)現(xiàn)文件傳輸系統(tǒng)

    QT基于TCP實(shí)現(xiàn)文件傳輸系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了QT基于TCP實(shí)現(xiàn)文件傳輸系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C++實(shí)現(xiàn)大數(shù)相乘的算法

    C++實(shí)現(xiàn)大數(shù)相乘的算法

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)大數(shù)相乘的算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • C語言水仙花數(shù)的實(shí)現(xiàn)

    C語言水仙花數(shù)的實(shí)現(xiàn)

    這篇文章主要介紹了C語言水仙花數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C++中move的使用及說明

    C++中move的使用及說明

    這篇文章主要介紹了C++中move的使用及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • c/c++內(nèi)存分配大小實(shí)例講解

    c/c++內(nèi)存分配大小實(shí)例講解

    在本篇文章里小編給大家整理了一篇關(guān)于c/c++內(nèi)存分配大小實(shí)例講解內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。
    2021-11-11
  • 初學(xué)C語言基本運(yùn)算和表達(dá)式

    初學(xué)C語言基本運(yùn)算和表達(dá)式

    在本文里小編給大家分享的是關(guān)于C語言基本運(yùn)算和表達(dá)式的相關(guān)知識點(diǎn),需要的朋友們跟著學(xué)習(xí)下。
    2019-08-08
  • C語言驅(qū)動(dòng)開發(fā)之內(nèi)核文件的讀寫

    C語言驅(qū)動(dòng)開發(fā)之內(nèi)核文件的讀寫

    這篇文章主要為大家詳細(xì)介紹了C語言驅(qū)動(dòng)開發(fā)中內(nèi)核文件的讀寫的系列函數(shù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • C++ 內(nèi)存分配處理函數(shù)set_new_handler的使用

    C++ 內(nèi)存分配處理函數(shù)set_new_handler的使用

    這篇文章主要介紹了C++ 內(nèi)存分配處理函數(shù)set_new_handler的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • C++中獲取UTC時(shí)間精確到微秒的實(shí)現(xiàn)代碼

    C++中獲取UTC時(shí)間精確到微秒的實(shí)現(xiàn)代碼

    本篇文章是對C++中獲取UTC時(shí)間精確到微秒的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評論

蓝田县| 武清区| 峨眉山市| 紫云| 孟津县| 吴桥县| 迁安市| 独山县| 新民市| 藁城市| 财经| 玉环县| 桐城市| 铜梁县| 桑植县| 图木舒克市| 桐梓县| 兴业县| 大田县| 房产| 康定县| 济南市| 伊金霍洛旗| 东兴市| 光山县| 西峡县| 淮滨县| 呼和浩特市| 乌鲁木齐县| 加查县| 红安县| 临泉县| 阜阳市| 盐源县| 开鲁县| 会宁县| 长沙县| 安康市| 湖南省| 喀什市| 南澳县|