[Qt-interest] tracking uniqueness

Andreas Pakulat apaku at gmx.de
Fri Apr 23 14:27:32 CEST 2010


On 23.04.10 11:29:01, Oliver Heins wrote:
> 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?

The only way I can see to make this work is use a send in the id for Token
in its constructor, i.e.:

Token( int id_ ) : id(id_) {}

and then have the member variable on Editor. There's no way to make a
counter for unique numbers per Editor instance without knowing the Editor
instance.

Andreas

-- 
Your ignorance cramps my conversation.



More information about the Qt-interest-old mailing list