C++探索構(gòu)造函數(shù)私有化會(huì)產(chǎn)生什么結(jié)果
提問(wèn):假設(shè)只有一個(gè)構(gòu)造方法,如果將之私有化會(huì)有什么后果
- 對(duì)于當(dāng)前類(lèi),它是無(wú)法實(shí)例化的
- 對(duì)于它的子類(lèi),子類(lèi)也是無(wú)法實(shí)例化的
構(gòu)造函數(shù)與是否能夠?qū)嵗嘘P(guān)
對(duì)于單個(gè)類(lèi)
正常情況下
#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();
}
編譯通不過(guò),因?yàn)闊o(wú)論是在棧還是堆上,都無(wú)法調(diào)用構(gòu)造函數(shù)來(lái)生成對(duì)象

私有化與繼承
正常情況下
#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. 父類(lèi)構(gòu)造函數(shù)私有化,而且子類(lèi)沒(méi)有提供public的構(gòu)造函數(shù)----》 子類(lèi)的構(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();
}
父類(lèi)構(gòu)造函數(shù)私有化,而且子類(lèi)提供public的構(gòu)造函數(shù)----》編譯還是不能通過(guò)
#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; //沒(méi)有作用
//此時(shí)子類(lèi)無(wú)法提供除了默認(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è)無(wú)法實(shí)例化的父類(lèi),不管子類(lèi)怎么折騰,都無(wú)法實(shí)例化。 這也是noncopyable類(lèi)的由來(lái)
成員變量與私有化
正常情況下
#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)前類(lèi)的某個(gè)成員變量是無(wú)法實(shí)例化的,那么當(dāng)前類(lèi)也無(wú)法實(shí)例化(正常,某個(gè)組件無(wú)法實(shí)例化,那么整個(gè)構(gòu)建就會(huì)出問(wèn)題)
#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();
}

解決方法:友元類(lèi)可以訪(fǎng)問(wèn)某個(gè)類(lèi)的私有成員,所以將令構(gòu)件為某個(gè)組件的友元類(lèi),這樣構(gòu)件就可以去訪(fǎng)問(wèn)組件私有的構(gòu)造函數(shù),將之構(gòu)造出來(lái)了
#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語(yǔ)言探索構(gòu)造函數(shù)私有化會(huì)產(chǎn)生什么結(jié)果的文章就介紹到這了,更多相關(guān)C語(yǔ)言構(gòu)造函數(shù)私有化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++類(lèi)與對(duì)象深入之構(gòu)造函數(shù)與析構(gòu)函數(shù)詳解
- C++超詳細(xì)講解拷貝構(gòu)造函數(shù)
- C++超詳細(xì)講解構(gòu)造函數(shù)與析構(gòu)函數(shù)的用法及實(shí)現(xiàn)
- C++分析構(gòu)造函數(shù)與析造函數(shù)的特點(diǎn)梳理
- c++詳細(xì)講解構(gòu)造函數(shù)的拷貝流程
- C++構(gòu)造函數(shù)的類(lèi)型,淺拷貝與深拷貝詳解
- C++類(lèi)繼承時(shí)的構(gòu)造函數(shù)
- C++淺析構(gòu)造函數(shù)的特性
- C++構(gòu)建函數(shù)使用介紹
相關(guān)文章
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)
這篇文章主要為大家詳細(xì)介紹了QT基于TCP實(shí)現(xiàn)文件傳輸系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C語(yǔ)言水仙花數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了C語(yǔ)言水仙花數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
初學(xué)C語(yǔ)言基本運(yùn)算和表達(dá)式
在本文里小編給大家分享的是關(guān)于C語(yǔ)言基本運(yùn)算和表達(dá)式的相關(guān)知識(shí)點(diǎn),需要的朋友們跟著學(xué)習(xí)下。2019-08-08
C語(yǔ)言驅(qū)動(dòng)開(kāi)發(fā)之內(nèi)核文件的讀寫(xiě)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言驅(qū)動(dòng)開(kāi)發(fā)中內(nèi)核文件的讀寫(xiě)的系列函數(shù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06
C++ 內(nèi)存分配處理函數(shù)set_new_handler的使用
這篇文章主要介紹了C++ 內(nèi)存分配處理函數(shù)set_new_handler的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
C++中獲取UTC時(shí)間精確到微秒的實(shí)現(xiàn)代碼
本篇文章是對(duì)C++中獲取UTC時(shí)間精確到微秒的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

