PDA

View Full Version : using a singleton when you will need more than one instance



mikro
8th October 2006, 17:42
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:


class cAnlagenArt {
public:
static cAnlagenArt* self( QString oid );
private:
static QMap<QString, cAnlagenArt* > _self;
}

and tried the following:

cAnlagenArt* cAnlagenArt::self(QString oid) {
if ( ! _self.contains(oid) )
_self[oid] = new cAnlagenArt( oid );
return _self[oid];
}

which gets me lots of
undefined reference to `cAnlagenArt::_self'

how else can i do this?

jpn
8th October 2006, 17:47
Have you defined the static member somewhere in the .cpp file?


QMap<QString, cAnlagenArt* > cAnlagenArt::_self;

mikro
8th October 2006, 17:49
noo, i thought i only needed to set pointers to 0 in this way. thank you