Results 1 to 3 of 3

Thread: using a singleton when you will need more than one instance

  1. #1
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default using a singleton when you will need more than one instance

    hi,
    i'd like to use a singleton for one of my classes. The class has an attribute named oid and i will need one instance for every value of oid - i just need to make sure there is no second one.
    so i tried:

    Qt Code:
    1. class cAnlagenArt {
    2. public:
    3. static cAnlagenArt* self( QString oid );
    4. private:
    5. static QMap<QString, cAnlagenArt* > _self;
    6. }
    To copy to clipboard, switch view to plain text mode 

    and tried the following:
    Qt Code:
    1. cAnlagenArt* cAnlagenArt::self(QString oid) {
    2. if ( ! _self.contains(oid) )
    3. _self[oid] = new cAnlagenArt( oid );
    4. return _self[oid];
    5. }
    To copy to clipboard, switch view to plain text mode 
    which gets me lots of
    undefined reference to `cAnlagenArt::_self'

    how else can i do this?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: using a singleton when you will need more than one instance

    Have you defined the static member somewhere in the .cpp file?
    Qt Code:
    1. QMap<QString, cAnlagenArt* > cAnlagenArt::_self;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    mikro (8th October 2006)

  4. #3
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using a singleton when you will need more than one instance

    noo, i thought i only needed to set pointers to 0 in this way. thank you

Similar Threads

  1. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 17:23
  2. Singleton pattern - end in recursion
    By probine in forum General Programming
    Replies: 6
    Last Post: 29th March 2006, 13:08
  3. creating a global instance
    By skatakam in forum General Programming
    Replies: 6
    Last Post: 24th February 2006, 16:26
  4. trying to use singleton pattern
    By therealjag in forum Newbie
    Replies: 3
    Last Post: 20th February 2006, 01:20
  5. Replies: 5
    Last Post: 16th January 2006, 05:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.