这段代码如何创建一个只有私有构造函数的类的实例?
我正在开发一个声音库(使用OpenAL),并从FMOD提供的界面中获取灵感,您可以看到界面 在此链接。
我提供了一些概念,例如:Sound、Channel 和 ChannelGroup,正如您通过 FMOD 接口所看到的,所有这些类都有一个私有构造函数,例如,如果您要创建一个 Sound,您必须使用函数 createSound () 由 System 类提供(如果您要创建 Channel 或 ChannelGroup,则相同)。
我想提供一个类似的机制,但我不明白它背后是如何工作的。例如,函数createSound()如何创建一个新的声音实例?构造函数是私有的,并且从 Sound 接口来看,没有任何静态方法或友谊。是否使用了一些模式?
编辑:只是为了让OP的问题清楚,他/她并不是问如何使用私有构造函数创建类的实例,问题在发布的链接中,如何创建具有私有构造函数且没有静态的类的实例方法或友元函数。
谢谢。
I'm working on a sound library (with OpenAL), and taking inspiration from the interface provided by FMOD, you can see the interface at this link.
I've provided some concepts like: Sound, Channel and ChannelGroup, as you can see through FMOD interface, all of those classes have a private constructor and, for example, if you would create a Sound you mast use the function createSound() provided by the System class (the same if you would create a Channel or a ChannelGroup).
I'd like to provide a similar mechanism, but I don't understand how it work behind. For example, how can the function createSound() create a new istance of a Sound? The constructor is private and from the Sound interface there aren't any static methods or friendship. Are used some patterns?
EDIT: Just to make OP's question clear, s/he is not asking how to create a instance of class with private constructor, The question is in the link posted, how is instance of classes created which have private constructor and NO static methods or friend functions.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不看源代码很难说。然而,FMOD 似乎是 100% C,带有全局变量,并且有一个糟糕的“OOP”C++ 包装器。
鉴于缺乏源代码以及 .h 文件中使用的一些糟糕技巧,代码可能是使用不同的头文件编译的,然后恰好可以与编译器一起工作(即使它显然是非标准的)他们正在使用。
我的猜测是,C++ 包装器的真正(未发布)源代码正在定义一个静态方法,或者如果一切确实只是全局的,那么该对象甚至没有真正创建,并且正在使用技巧来愚弄 C++ 对象系统,让其认为存在确实是一个物体。显然,所有调度都是静态的,因此这(虽然不是正式合法的)无论如何都可以与我所知道的 C++ 实现一起工作。
无论他们做了什么,从 C++ 的角度来看,这都是相当丑陋且不符合规范的。
Hard to say without seeing the source code. Seems however that FMOD is 100% C with global variables and with a bad "OOP" C++ wrapper around it.
Given the absence of source code and a few of the bad tricks that are played in the .h files may be the code is compiled using a different header file and then just happens to work (even if it's clearly non-standard) with the compilers they are using.
My guess is that the real (unpublished) source code for the C++ wrapper is defining a static method or alternatively if everything is indeed just global then the object is not really even created and tricks are being played to fool C++ object system to think there is indeed an object. Apparently all dispatching is static so this (while not formally legal) can happen to work anyway with C++ implementations I know.
Whatever they did it's quite ugly and non-conforming from a C++ point of view.
他们从不创建任何实例!工厂函数就在头文件中。
您传入以获取 System object 的指针会立即转换为指向 fmod.h 头文件中声明的 C 结构体的指针。
因为它是一个没有任何数据成员的类,谁能区分?
They never create any instances! The factory function is right there in the header
The pointer you pass in to get a System object is immediately cast to a pointer to a C struct declared in the fmod.h header.
As it is a class without any data members who can tell the difference?
create_alpha 本来可以是一个声明为
friend
的自由函数,但这只是污染了命名空间。恐怕我无法访问该链接,但另一种方式可能是工厂模式。我现在有点猜测。
create_alpha could have been a free function declared
friend
but that's just polluting the namespace.I'm afraid I can't access that link but another way could be a factory pattern. I'm guessing a bit, now.
正如他们的评论所说,这是工厂模式。
很难确切地说发生了什么,因为他们没有发布
FMOD_System_Create
方法的源代码。工厂模式是一种创建对象的机制,但生成的(子)类取决于工厂调用的参数。 http://en.wikipedia.org/wiki/Factory_method_pattern
It is the factory pattern - as their comment says.
It's difficult to say exactly what is happening as they don't publish the source for the
FMOD_System_Create
method.The factory pattern is a mechanism for creating an object but the (sub)class produced depends on the parameters of the factory call. http://en.wikipedia.org/wiki/Factory_method_pattern