如何在C ++中使用Privaet Constructor创建对象 [英] how to make object in Privaet Constructor in C++

查看:126
本文介绍了如何在C ++中使用Privaet Constructor创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone!



i是C ++中的新程序员,请告诉我在C ++中使用私有构造函数有什么用途,并且< b>如何在C ++中创建私有构造函数中的对象,请告诉我是否有关于私有构造函数的好主意。

Hello Everyone!

i am new programmer in C++,Please tell me what is the use of Private Constructor in C++,And "how to create object in Private Constructor" in C++,please tell me if any have Good Idea About the private Constructor.

推荐答案

目的使构造函数私有化是为了不可能直接使用类外部的构造函数创建类的实例。

实例只能在类或朋友类中创建。 br />


这通常用于Singleton模式,以强制类的用户调用实例创建的特殊方法。



Singleton Pattern&它用C ++实现 [ ^ ]



C ++中的Singleton: 之前和之后[ ^ ]


在私有构造函数中创建对象时,可以采用与公共构造函数相同的方式进行。
The purpose of making the constructor private is to make it impossible to create an instance of the class directly using the constructor from outside the class.
The instance can only be created from within the class or a friend class.

This is usually used in the Singleton Pattern in order to force the a user of the class to call a special method for instance creation.

Singleton Pattern & its implementation with C++[^]

Singleton in C++: Before and after[^]

When it comes to creating objects in a private constructor, you do it in the same fashion as for a public constructor.


这篇关于如何在C ++中使用Privaet Constructor创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆