Problem creating singleton plugin
-
I am trying to create a plugin that allows only one instance to run. It does this by having a static "instance" (named singleton) of the object and making the constructor private. The only way to create an object is to call a static method with the following code:
@
// from header:
static ContentMode* GetAppMode(IAppHost* host);// from cpp source:
ContentMode* ContentMode::GetAppMode(IAppHost* host)
{
if (singleton == NULL)
singleton = new ContentMode();
return singleton;
}
@The problem is that when I compile (in Visual Studio) I get the following error:
error C2248: 'AppCore::ContentMode::ContentMode' : cannot access private member declared in class 'AppCore::ContentMode' D:\Developing\Projects\IMT\Rosweld\trunk\Source\MyApp\ContentMode\GeneratedFiles\Debug\moc_ContentMode.cppThe line referred to that generates this error is in the moc file:
@
using namespace AppCore;
QT_MOC_EXPORT_PLUGIN(AppCore::ContentMode, ContentMode)
@Since this is a file that is automatically generated, I am going to need to use a different approach. Is there a way around this issue or do I need to make a public constructor and somehow inhibit more than one instance a different way?
-
[quote]
@
// from header:
static ContentMode* GetAppMode(IAppHost* host);
@
[/quote]
I assume that you declared this function inside the class declaration.
The code that you posted looks good. So the error is in the part that you have not posted.As an alternative approach you can take a look on "QGlobalStatic":http://qt-project.org/doc/qt-5/QGlobalStatic.html#details
-
Yes. And of course it is (the code looks good). There is no complaint about the code I posted. However, I specifically said that the constructor is private. That is the source of the error and the point of having the static method. Making the constructor public defeats the purpose of having a singleton class (a class that there can only be one instance of) so either there needs to be a way around this, or I am going to have to come up with another mechanism for accomplishing this goal.