Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Template class creating object

    C++ Gurus
    4
    6
    1494
    Loading More Posts
    • 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.
    • J
      jcga last edited by VRonin

      Hello,

      My question concerns C++ template classes rather than specific Qt points, so I'm wondering if I can post it here. Sorry if not.

      Here is my problem. I have a template class derived from C++ 'list':

      Template <class T>
      class ObjListP : public list<T>
      {
        // class implementation
      };
      

      I want this class to store only pointers to objects of class Obj (or its derivate classes), so I declare a list object

      ObjListP<Obj*> mylist();
      

      so that T is in fact a pointer Obj*.

      Now, in a method of the above class, I want to create a new object of class T. Obviously

      T obj=new T();
      

      is wrong, because it creates a pointer to a pointer, that is an Obj** not an Obj*. I can create it as

      Obj* obj=new Obj();
      

      but this will not create pointers to objects in the classes derived from Obj if T is in fact a derived class.

      Is there a way to create an object that is a pointer to an object of class T?

      Thanks, and sorry if my question is misplaced.

      JClaude

      jsulm 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @jcga last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion @jcga last edited by jsulm

          @jcga said in Template class creating object:

          Now, in a method of the above class, I want to create a new object of class T. Obviously
          T obj=new T();

          Why do you want to do this? If you only want to manage pointers - why then do you create instances in this template?
          For C++ questions you can use https://forum.qt.io/category/34/c-gurus

          1 Reply Last reply Reply Quote 2
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            Hi
            Im not sure i understand question.
            Say we have the following code

            class Obj {
             public:
              virtual void print() { qDebug() << "obj"; }
            };
            
            class SubObj : public Obj {
             public:
              virtual void print() { qDebug() << "subobj"; }
            };
            
            template <class T>
            class ObjListP : public std::vector<T> {
              // class implementation
            };
            

            and we call it like

              ObjListP<Obj*> mylist;
              mylist.push_back( new Obj);
              mylist.push_back( new SubObj);
            
              mylist[0]->print();
              mylist[1]->print();
            

            we get
            obj
            subobj

            so not sure what you mean it wont create the right type.
            Or what the actual issue it.

            jsulm 1 Reply Last reply Reply Quote 1
            • jsulm
              jsulm Lifetime Qt Champion @mrjj last edited by

              @mrjj He wants to use a pointer as T, so

              ObjListP<Obj*> mylist;
              

              and he wants to create instances of T inside the ObjListP class. But this is bad design in my opinion, because ObjListP class is managing pointers to T and should not create instances of T by itself. This is something users of ObjListP will do (as with any other container).

              1 Reply Last reply Reply Quote 4
              • VRonin
                VRonin last edited by VRonin

                I want this class to store only pointers to objects of class Obj

                Add, inside the class
                static_assert(std::is_pointer<T>::value && std::is_base_of<Obj, std::remove_pointer<T>::type>::value,"T must be a pointer to an Obj");

                Is there a way to create an object that is a pointer to an object of class T?

                T obj = new std::remove_pointer<T>::type;

                You should also have a think about what happens if the user const-qualifies T. For example, in case of ObjListP<const Obj*> mylist;

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply Reply Quote 4
                • First post
                  Last post