How does this code instantiate a class that only has a private constructor?

I am working on a sound library (with OpenAL) and am inspired by the interface provided by FMOD, you can see the interface at this link .

I introduced several concepts such as Sound, Channel and ChannelGroup, as you can see through the FMOD interface, all these classes have a private constructor and, for example, if you create the sound that you use, use the createSound () function provided by the System ( same if you create a channel or group of channels).

I would like to provide a similar mechanism, but I do not understand how it works. For example, how can the createSound () function create a new sound value? The constructor is private, and there are no static methods or friendships from the Sound interface. Are some templates used?

EDIT: just to make the OP question clear, he / she does not ask how to instantiate a class with a private constructor. The question is in the posted link. How to create an instance of classes that have a private constructor and NO static methods or functions of friends.

Thank.

+5
source share
4 answers

It's hard to say without seeing the source code. It seems that FMOD is 100% C with global variables and with a bad OOP C ++ wrapper around it.

, .h, , , ( ) .

, () ++- , , , , , ++ , . , , ( ) ++, .

, ++.

+7

! factory

/*
    FMOD System factory functions.
*/
inline FMOD_RESULT System_Create(System **system)
{ return FMOD_System_Create((FMOD_SYSTEM **)system); }

, , System, C-, fmod.h.

- , ?

+1
struct Foo {
    enum Type {
        ALPHA,
        BETA_X,
        BETA_Y
    };
    Type type () const;
    static Foo alpha (int i) {return Foo (ALPHA, i);}
    static Foo beta  (int i) {return Foo (i<0 ? BETA_X : BETA_Y, i);}
private:
    Foo (Type, int);
};

create_alpha friend, .

, , factory. .

0

factory, .

/*
    FMOD System factory functions.
*/
inline FMOD_RESULT System_Create(System **system) { return FMOD_System_Create((FMOD_SYSTEM **)system); }

, , FMOD_System_Create.

The factory template is a mechanism for creating an object, but the created class (sub) depends on the parameters of the factory call. http://en.wikipedia.org/wiki/Factory_method_pattern

0
source

All Articles