Template class creating object



  • 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


  • Moderators

    This post is deleted!

  • Moderators

    @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


  • Qt Champions 2017

    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.


  • Moderators

    @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).



  • 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;


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.