C/C++编程:构造函数私有化会有什么后果

1059 篇文章 278 订阅

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

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

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

准备

对于单个类

  1. 正常情况下
#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();
}

在这里插入图片描述

  1. 构造函数私有化
#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();
}

编译通不过,因为无论是在栈还是堆上,都无法调用构造函数来生成对象
在这里插入图片描述

私有化与继承

  1. 正常情况下
#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();
}

在这里插入图片描述

  1. 父类构造函数私有化,而且子类提供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类的由来

成员变量与私有化

  1. 正常情况下
#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();
}

在这里插入图片描述

  1. 解决方法:友元类可以访问某个类的私有成员,所以将令构件为某个组件的友元类,这样构件就可以去访问组件私有的构造函数,将之构造出来了
#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();
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当涉及到面向对象编程时,C++中的类是一种用于封装数据和方法的抽象数据类型。类提供了一种组织和管理相关数据和函数的方式,以便更好地模拟现实世界中的对象。 类中包含了两个主要部分:成员变量和成员函数。成员变量是存储在类内部的数据,用于表示对象的状态或属性。成员函数是在类内部定义的函数,用于操作和访问类的成员变量。 构造函数是一种特殊类型的成员函数,用于在创建对象时初始化对象的数据成员。构造函数与类名相同,并且没有返回类型。它可以具有参数或不带参数。构造函数在对象创建时自动调用,并可以执行必要的初始化操作。 构造函数有以下几个重要特点: 1. 构造函数在对象创建时自动调用,无需显式调用。 2. 构造函数具有与类相同的名称。 3. 构造函数可以重载,即同一个类可以有多个构造函数,只要它们的参数列表不同即可。 4. 构造函数可以具有默认参数,这样在创建对象时可以省略一些参数。 5. 构造函数可以执行任意的初始化操作,例如分配内存、设置默认值等。 下面是一个示例代码,其中定义了一个名为`Person`的类,并包含一个带参数的构造函数: ```cpp class Person { private: std::string name; int age; public: Person(const std::string& n, int a) { name = n; age = a; } void displayInfo() { std::cout << "Name: " << name << ", Age: " << age << std::endl; } }; ``` 在上述示例中,`Person`类有两个私有成员变量`name`和`age`,并且定义了一个带参数的构造函数`Person(const std::string& n, int a)`。构造函数使用参数来初始化成员变量。类还包含一个公有成员函数`displayInfo()`,用于显示对象的信息。 通过使用构造函数,我们可以创建`Person`类的对象并传递必要的参数来初始化对象的成员变量。以下是一个示例代码: ```cpp Person person1("Alice", 25); person1.displayInfo(); // 输出: Name: Alice, Age: 25 Person person2("Bob", 30); person2.displayInfo(); // 输出: Name: Bob, Age: 30 ``` 在上述示例中,通过调用构造函数并传递参数来创建了两个`Person`对象,并使用`displayInfo()`函数显示了对象的信息。 这就是C++中类和构造函数的简单介绍。它们是面向对象编程中重要的概念,用于封装数据和方法,并提供了更好的代码组织和可重用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值