Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Template class creating object
Forum Updated to NodeBB v4.3 + New Features

Template class creating object

Scheduled Pinned Locked Moved Unsolved C++ Gurus
6 Posts 4 Posters 2.2k Views 1 Watching
  • 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 Offline
    J Offline
    jcga
    wrote on last edited by VRonin
    #1

    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

    jsulmJ 2 Replies Last reply
    0
    • J jcga

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • J jcga

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @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
        2
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          jsulmJ 1 Reply Last reply
          1
          • mrjjM mrjj

            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.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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
            4
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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
              4

              • Login

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