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. [CLOSED]C++, adding uniqueID to every class instance
Forum Update on Monday, May 27th 2025

[CLOSED]C++, adding uniqueID to every class instance

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.3k Views
  • 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.
  • M Offline
    M Offline
    maximus
    wrote on 30 Oct 2013, 20:21 last edited by
    #1

    This is a basic C++ question, I would like to add an unique ID to every instance of a class. I use to this so easily with Java, here i just wasted 1 hrs. grr
    Thanks if you can help...

    Workout.H
    @#ifndef WORKOUT_H
    #define WORKOUT_H

    #include <QString>
    #include <QList>
    #include "interval.h"

    class Workout
    {

    public:

    Workout(QList<std::shared_ptr<Interval>>, QString name, QString createdBy, Type type);
    int getID();
    

    //----------------------------------------------
    private :

    static int s_nIDGenerator;
    int m_nID;
    

    };

    #endif // WORKOUT_H@

    Workout.CPP
    @Workout::Workout(QList<std::shared_ptr<Interval>> lstInterval, QString name, QString createdBy, Type type) {

    m_nID = s_nIDGenerator++;
    qDebug() << "ID HERE:" << m_nID;
    

    ...
    }@

    Error log :
    workout.obj:-1: error: LNK2019: unresolved external symbol "private: static int Workout::s_nIDGenerator" (?s_nIDGenerator@Workout@@0HA) referenced in function "public: __thiscall Workout::Workout(class QList<class std::tr1::shared_ptr<class Interval> >,class QString,class QString,enum Workout::Type)" (??0Workout@@QAE@V?$QList@V?$shared_ptr@VInterval@@@tr1@std@@@@VQString@@1W4Type@0@@Z)

    [Edit]
    I used the code "here":http://www.learncpp.com/cpp-tutorial/811-static-member-variables/ that is supposed to work..


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on 30 Oct 2013, 20:36 last edited by
      #2

      closed.. I will keep a pointer instead of a unique Id
      Thanks


      Free Indoor Cycling Software - https://maximumtrainer.com

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 30 Oct 2013, 20:41 last edited by
        #3

        In C++ there is a clearer separation of declaration and definition than in Java. You have declared the static s_nIDGenerator but you didn't define it.
        Declaration is part of the interface. Definition is part of implementation.

        Here's an example how to do it:
        @
        //whatever.h
        class Whatever {
        ...
        static int someInt; //this is the declaration
        }

        //whatever.cpp
        int Whatever::someInt = 1; //this is the definition
        @

        But it's actually easier (and cleaner in my opinion) to use a static local variable than a static member. This way you'll have whole id code in one place:

        @
        //whatever.h
        class Whatever {
        int id;
        public:
        Whatever();
        inline int getId() const { return id; }
        };

        //whatever.cpp
        Whatever::Whatever() {
        static int counter = 1;
        id = counter++;
        }
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on 30 Oct 2013, 20:59 last edited by
          #4

          Thanks a lot.. I used your second version and works fine


          Free Indoor Cycling Software - https://maximumtrainer.com

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NicuPopescu
            wrote on 31 Oct 2013, 16:42 last edited by
            #5

            I think an id like that is limited because if you pass it to a function alone or even with the class type you cannot extract the class instance from it without an additional construct. imho you should learn and use what are handles? a handle is an identifier storing the address of the pointer by which you could dereference to the pointer itself (on net there is much confusion on this)

            for instance, in windows world in frameworks like MFC or even Qt handles are used for attaching their classes to the native window structures ...

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maximus
              wrote on 31 Oct 2013, 17:42 last edited by
              #6

              Hey again Nicu,

              I will certainly check into that.
              My use of the unique ID was a workaround, I had to learn how work QModelIndex and finally i'm good without using id.
              Before I was using a QHash<key, object> and I wanted the key to be unique to get object fast retrieval "0(1)".

              See you


              Free Indoor Cycling Software - https://maximumtrainer.com

              1 Reply Last reply
              0

              1/6

              30 Oct 2013, 20:21

              • Login

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