[Qt-interest] tracking uniqueness

Oliver Heins heins at sopos.org
Fri Apr 23 11:29:01 CEST 2010


I have several instances of Editor, each of which can contain a lot of
Token.  Each Token token should have an id that is unique to it's
instance of Editor.  For this, I keep track with an static integer
Token::lastuniq.

class Token
{
public: 
    Token() { ++lastuniq }
private: 
    static quint32 lastuniq;
};

class Editor
{
    QVector<Token> tokenVec;
}

quint32 Token::lastuniq = 0;


Unfortunately, that means that there is only one instance lastuniq.  But
I like to have several instances, one for each editor.

I tried to solve it by nesting Token to Editor:

class Editor
{
    class Token
    {
    public:
        Token() { ++lastuniq }
    private:
        static quint32 lastuniq;
    }       

    QVector<Token> tokenVec;
};

quint32 Editor::Token::lastuniq = 0;


However, Editor::Token::lastuniq is not only static to Editor::Token,
but to Editor itself.  It would seem to feel natural to make lastuniq a
member of Editor, but then I couldn't access it from Token().

Am I missing something?  Is it possible to achieve this with signals and
slots?  Or is my idea of tracking the uniqueness this way a bad one?

TIA,
 olli



More information about the Qt-interest-old mailing list