C++探索构造函数私有化会产生什么结果

 更新时间:2022年05月18日 11:13:40   作者:OceanStar的学习笔记  
C++的构造函数的作⽤:初始化类对象的数据成员。即类的对象被创建的时候,编译系统对该对象分配内存空间,并⾃动调⽤构造函数,完成类成员的初始化。构造函数的特点:以类名作为函数名,⽆返回类型

提问:假设只有一个构造方法,如果将之私有化会有什么后果

  • 对于当前类,它是无法实例化的
  • 对于它的子类,子类也是无法实例化的

构造函数与是否能够实例化有关

对于单个类

正常情况下

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

构造函数私有化

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

编译通不过,因为无论是在栈还是堆上,都无法调用构造函数来生成对象

私有化与继承

正常情况下

#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. 父类构造函数私有化,而且子类没有提供public的构造函数----》 子类的构造函数也是私有化的

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

父类构造函数私有化,而且子类提供public的构造函数----》编译还是不能通过

#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;  //没有作用
 	//此时子类无法提供除了默认构造函数之外的函数,比如 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();
}

结论:只要继承了一个无法实例化的父类,不管子类怎么折腾,都无法实例化。 这也是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. 如果当前类的某个成员变量是无法实例化的,那么当前类也无法实例化(正常,某个组件无法实例化,那么整个构建就会出问题)

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

解决方法:友元类可以访问某个类的私有成员,所以将令构件为某个组件的友元类,这样构件就可以去访问组件私有的构造函数,将之构造出来了

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

到此这篇关于C语言探索构造函数私有化会产生什么结果的文章就介绍到这了,更多相关C语言构造函数私有化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++ main函数的几点细节

    C++ main函数的几点细节

    这篇文章主要介绍了C++ main函数的几点细节,帮助大家更好的理解和学习C++,感兴趣的朋友可以了解下
    2020-08-08
  • QT基于TCP实现文件传输系统

    QT基于TCP实现文件传输系统

    这篇文章主要为大家详细介绍了QT基于TCP实现文件传输系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • C++实现大数相乘的算法

    C++实现大数相乘的算法

    这篇文章主要为大家详细介绍了C++实现大数相乘的算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • C语言水仙花数的实现

    C语言水仙花数的实现

    这篇文章主要介绍了C语言水仙花数的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • C++中move的使用及说明

    C++中move的使用及说明

    这篇文章主要介绍了C++中move的使用及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。
    2021-11-11
  • 初学C语言基本运算和表达式

    初学C语言基本运算和表达式

    在本文里小编给大家分享的是关于C语言基本运算和表达式的相关知识点,需要的朋友们跟着学习下。
    2019-08-08
  • C语言驱动开发之内核文件的读写

    C语言驱动开发之内核文件的读写

    这篇文章主要为大家详细介绍了C语言驱动开发中内核文件的读写的系列函数,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-06-06
  • C++ 内存分配处理函数set_new_handler的使用

    C++ 内存分配处理函数set_new_handler的使用

    这篇文章主要介绍了C++ 内存分配处理函数set_new_handler的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • C++中获取UTC时间精确到微秒的实现代码

    C++中获取UTC时间精确到微秒的实现代码

    本篇文章是对C++中获取UTC时间精确到微秒的实现进行了详细的分析介绍,需要的朋友参考下
    2013-05-05

最新评论