Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this code create an instance of a class which has only a private constructor?

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.

like image 257
enigma Avatar asked Jul 07 '11 07:07

enigma


People also ask

How can we create object of a class having private constructor?

Java allows us to declare a constructor as private. We can declare a constructor private by using the private access specifier. Note that if a constructor is declared private, we are not able to create an object of the class. Instead, we can use this private constructor in Singleton Design Pattern.

How do you create an instance of a class having a private constructor in Java?

First, initiate a constructor as private. Then create a private static instance of this singleton class. Keep in mind to NOT instantiate it. Then, write a static method, which checks the static instance member for null and initiates the instance.

Can we create instance of private constructor?

Can you create object of class with private constructor in C#? No, object of a class having private constructor cannot be instantiated from outside of the class.

Can we create instance of private constructor in Java?

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.


1 Answers

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.

like image 160
6502 Avatar answered Sep 28 '22 10:09

6502