服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++探索构造函数私有化会产生什么结果

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

2022-12-05 13:35OceanStar的学习笔记 C/C++

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

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

构造函数私有化

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

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

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

 

私有化与继承

正常情况下

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

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

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

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

父类构造函数私有化,而且子类提供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();
}

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

结论:只要继承了一个无法实例化的父类,不管子类怎么折腾,都无法实例化。 这也是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();
}

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

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

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

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

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

原文链接:https://blog.csdn.net/zhizhengguan/article/details/124727948

延伸 · 阅读

精彩推荐
  • C/C++C++实现简单推箱子小游戏

    C++实现简单推箱子小游戏

    这篇文章主要为大家详细介绍了C++实现简单推箱子小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    变强也变秃了10112021-09-26
  • C/C++C语言实现宾馆管理系统课程设计

    C语言实现宾馆管理系统课程设计

    这篇文章主要为大家详细介绍了C语言实现宾馆管理系统课程设计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    苏尧木子6632022-10-21
  • C/C++关于C++静态数据成员的实现讲解

    关于C++静态数据成员的实现讲解

    今天小编就为大家分享一篇关于关于C++静态数据成员的实现讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小...

    Engineer-Bruce_Yang8502021-07-13
  • C/C++C++常量指针,指针常量,指向常量的常指针详解

    C++常量指针,指针常量,指向常量的常指针详解

    刚接触到指针时,关于C++常量指针,指针常量,指向常量的常指针容易混淆,所以整理下,希望能够帮助自己也帮助到大家...

    Z小旋4212022-01-25
  • C/C++详解C语言面向对象编程中的封装

    详解C语言面向对象编程中的封装

    这篇文章主要为大家详细介绍了C语言面向对象编程中的封装,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望...

    Silent Knight4332022-10-21
  • C/C++详谈c++跨平台编码的问题

    详谈c++跨平台编码的问题

    下面小编就为大家带来一篇详谈c++跨平台编码的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    selfree12412021-05-28
  • C/C++一篇文章教你在C++中操作符可分为哪几种类和用法

    一篇文章教你在C++中操作符可分为哪几种类和用法

    这篇文章主要介绍了C++编程中操作符的种类和用法,是C++入门学习中的基础知识,需要的朋友可以参考下,希望能够给你带来帮助...

    乾县.彭于Yan8852022-01-10
  • C/C++全面了解#pragma once与 #ifndef的区别

    全面了解#pragma once与 #ifndef的区别

    下面小编就为大家带来一篇全面了解#pragma once与 #ifndef的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    junjie12332021-04-15