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语言构造函数私有化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Qt音视频开发之利用ffmpeg实现倍速播放

    Qt音视频开发之利用ffmpeg实现倍速播放

    这篇文章主要为大家详细介绍了在Qt音视频开发中如何利用ffmpeg实现倍速播放功能(半倍速/2倍速/4倍速/8倍速),感兴趣的小伙伴可以了解一下
    2022-11-11
  • Qt sender()函数的具体使用

    Qt sender()函数的具体使用

    在处理信号时,Qt提供了一个特殊的函数sender(),可以返回发送信号的对象指针,以实现更灵活的代码逻辑,本文就来介绍一下Qt sender()函数的具体使用,感兴趣的可以了解一下
    2024-01-01
  • C++双目运算符+=的重载详解

    C++双目运算符+=的重载详解

    这篇文章主要介绍了详解C++编程中的双目运算符重载,是C++入门学习中的基础知识,需要的朋友可以参考下,希望能够给你带来帮助
    2021-09-09
  • 解析C/C++指针、函数、结构体、共用体

    解析C/C++指针、函数、结构体、共用体

    这篇文章主要介绍了C/C++指针、函数、结构体、共用体的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • C++构造函数详解

    C++构造函数详解

    这篇文章主要介绍了C++构造函数详解,上一篇文章我们介绍了定义了类,在使用之前,往往还需要对类进行初始化。这篇介绍的就是对类进行初始化的方法,需要的朋友可以参考一下
    2022-01-01
  • 利用c++和easyx图形库做一个低配版扫雷游戏

    利用c++和easyx图形库做一个低配版扫雷游戏

    这篇文章主要介绍了用c++和easyx图形库做一个低配版扫雷游戏,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • c++ 实现文件逐行读取与字符匹配

    c++ 实现文件逐行读取与字符匹配

    这里尝试通过C++来实现一个文件IO的功能,看看是否能够比python的表现更好一些,感兴趣的朋友可以参考下
    2021-05-05
  • 基于C语言字符串函数的一些使用心得

    基于C语言字符串函数的一些使用心得

    以下是对C语言中字符串函数的一些使用心得进行了详细的介绍,需要的朋友可以过来参考下
    2013-08-08
  • 用typedef定义类型详细总结

    用typedef定义类型详细总结

    用typedef可以声明各种类型名,但不能用来定义变量,用typedef可以声明数组类型、字符串类型、使用比较方便
    2013-10-10
  • OpenCV实现相机标定板

    OpenCV实现相机标定板

    这篇文章主要为大家详细介绍了OpenCV实现相机标定板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04

最新评论