What does typedef Class *(createClassFunction)(void)
(or another variation is typedef Class* (__stdcall *CreateClassFunction)(void)
)stand for?
What does it mean? How am I supposed to explain it?
Especially in the context of Factory Patterns...

- 39
- 10
-
2Read up on function pointers. – Paul R Aug 22 '14 at 09:57
-
Defines function pointer type named createClassFunction that returns pointer to Class, so that you can declare a variable of type createClassFunction, and assign an actual implementation of such function, so that each time one is invoked, it e.g. instantiates Class object. – Piotr Skotnicki Aug 22 '14 at 09:58
-
To complete this: the `__stdcall` modifier tells the compiler to use a specific calling convention (how to pass parameters, who is responsible for stack cleanup). However this is usually completely uninteressting for highlevel code and the compiler will usually choose the right one for you. Besides this it is also often possible to tell the compiler to make every call a `__fastcall` if its possible via compiler flags; another reason to leave the calling convention unspecified. – Sebastian Hoffmann Aug 22 '14 at 10:02
1 Answers
createClassFunction
is a typedef for a function taking no arguments and returning a Class *
.
With that declaration, a pointer to such a funciton can obviously act as factory for a Class
. Usage might be as follows:
// the class factory
Class * MostTrivialFactoryKnownToMan()
{
return new Class;
}
// another class factory
Class * CreateClassWithLog()
{
ClassWithLog * p = new ClassWithLog; // derived from Class
p->SetLogFile("log.txt");
return p;
}
// code consuming the class factory
void PopulateStars(createClassFunction * factory)
{
// creates many instances of `Class` through `factory`
Sky * sky = GetSky();
for(int i=0; i<sky->GetStarCount(); ++i)
{
Class * inhabitant = (*factory)();
sky->GetStar(i)->SetInhabitant(inhabitant);
}
}
// code deciding which factory to use
const bool todayWeWriteALog = (rand() %2) != 0;
createClassFunction * todaysFactory = todayWeWriteALog ?
&MostTrivialFactoryKnownToMan :
&CreateClassWithLog);
PopulateStars(factory);
__stdcall
is a compiler specific attribute changin the calling convention (how parameters and return value are passed between caller and implementation). This is often important for binary compatibility - e.g. when a Pascal program needs to call a funciton imlemented in C or C++.
Issues:
The factory function returns a raw pointer. There must be an implicit contract between the factory and the consumer how to free that pointer (e.g. through delete
in or example).
Using a smart pointer (e.g. shared_ptr) for the return type would allow the factory to determine the deletion policy.
The factory, as a function pointer, may not hold state (such as the log file name, it needs to be hard coded in the function, or available globally). using a callable object instead would allow to implement configurable factories.

- 40,917
- 20
- 104
- 186