Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem creating singleton plugin
Forum Updated to NodeBB v4.3 + New Features

Problem creating singleton plugin

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    primem0ver
    wrote on last edited by
    #1

    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.cpp

    The 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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      [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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        primem0ver
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved