单类实例C ++ [英] Single class instance C++

查看:108
本文介绍了单类实例C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个只能构造一次的类?如果您尝试创建其他实例,则会出现编译时错误。

Is it possible to create a class which could be constructed just one time? If you would try to create an other instance of it, a compile-time error should occure.

推荐答案

实例化是动态的,运行。编译错误在编译时。所以答案是否定的,不可能在任何第二次实例化时获得编译错误。

Instantiation is dynamic, at run time. Compilation errors are at compile time. So the answer is no, it's not possible to get a compilation error on any second instantiation.

然而,您可以使用单例,但是请仔细考虑它是否真的需要。

You can however use a singleton, but do consider very carefully whether it's really needed.

这篇关于单类实例C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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