欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

  • 對于當前類,它是無法實例化的
  • 對于它的子類,子類也是無法實例化的

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

對于單個類

正常情況下

#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();
}

編譯通不過,因為無論是在棧還是堆上,都無法調(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;  //沒有作用
 	//此時子類無法提供除了默認構(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é)論:只要繼承了一個無法實例化的父類,不管子類怎么折騰,都無法實例化。 這也是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. 如果當前類的某個成員變量是無法實例化的,那么當前類也無法實例化(正常,某個組件無法實例化,那么整個構(gòu)建就會出問題)

#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òu)件為某個組件的友元類,這樣構(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ù)私有化會產(chǎn)生什么結(jié)果的文章就介紹到這了,更多相關(guān)C語言構(gòu)造函數(shù)私有化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Qt音視頻開發(fā)之利用ffmpeg實現(xiàn)倍速播放

    Qt音視頻開發(fā)之利用ffmpeg實現(xiàn)倍速播放

    這篇文章主要為大家詳細介紹了在Qt音視頻開發(fā)中如何利用ffmpeg實現(xiàn)倍速播放功能(半倍速/2倍速/4倍速/8倍速),感興趣的小伙伴可以了解一下
    2022-11-11
  • Qt sender()函數(shù)的具體使用

    Qt sender()函數(shù)的具體使用

    在處理信號時,Qt提供了一個特殊的函數(shù)sender(),可以返回發(fā)送信號的對象指針,以實現(xiàn)更靈活的代碼邏輯,本文就來介紹一下Qt sender()函數(shù)的具體使用,感興趣的可以了解一下
    2024-01-01
  • C++雙目運算符+=的重載詳解

    C++雙目運算符+=的重載詳解

    這篇文章主要介紹了詳解C++編程中的雙目運算符重載,是C++入門學習中的基礎(chǔ)知識,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-09-09
  • 解析C/C++指針、函數(shù)、結(jié)構(gòu)體、共用體

    解析C/C++指針、函數(shù)、結(jié)構(gòu)體、共用體

    這篇文章主要介紹了C/C++指針、函數(shù)、結(jié)構(gòu)體、共用體的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • C++構(gòu)造函數(shù)詳解

    C++構(gòu)造函數(shù)詳解

    這篇文章主要介紹了C++構(gòu)造函數(shù)詳解,上一篇文章我們介紹了定義了類,在使用之前,往往還需要對類進行初始化。這篇介紹的就是對類進行初始化的方法,需要的朋友可以參考一下
    2022-01-01
  • 利用c++和easyx圖形庫做一個低配版掃雷游戲

    利用c++和easyx圖形庫做一個低配版掃雷游戲

    這篇文章主要介紹了用c++和easyx圖形庫做一個低配版掃雷游戲,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • c++ 實現(xiàn)文件逐行讀取與字符匹配

    c++ 實現(xiàn)文件逐行讀取與字符匹配

    這里嘗試通過C++來實現(xiàn)一個文件IO的功能,看看是否能夠比python的表現(xiàn)更好一些,感興趣的朋友可以參考下
    2021-05-05
  • 基于C語言字符串函數(shù)的一些使用心得

    基于C語言字符串函數(shù)的一些使用心得

    以下是對C語言中字符串函數(shù)的一些使用心得進行了詳細的介紹,需要的朋友可以過來參考下
    2013-08-08
  • 用typedef定義類型詳細總結(jié)

    用typedef定義類型詳細總結(jié)

    用typedef可以聲明各種類型名,但不能用來定義變量,用typedef可以聲明數(shù)組類型、字符串類型、使用比較方便
    2013-10-10
  • OpenCV實現(xiàn)相機標定板

    OpenCV實現(xiàn)相機標定板

    這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)相機標定板,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04

最新評論