3

I want to have a QList or QVector in a class that it has a constant number of element type QScopedPointer< SomeAbstractClass >. I could easily use something like this:

QList<QScopedPointer<SomeClass> listClass;
listClass.append(QScopedPointer<SomeClass>(new SomeClass()));

But in QScopedPointer, copy constructor is disabled and we can't instantiate an abstract class.

Off course I could easily use QSharedPointer but for some reasons I don't want to do that, so what can I do?

Edit: Here is my sample code, and I got this error:

class someAbsClass{
    public:
        someAbsClass(){}
        virtual void fcn()=0;
    };

    class someClass : public someAbsClass{
    public:
        someClass(){}
        void fcn(){
            std::cout << "fcn" << std::endl;
        }
    };


    int main(){
        QVector<QScopedPointer<someAbsClass> > VectorPtrAbss(1);
        VectorPtrAbss[0].reset(new someClass());
    }

error: 'QScopedPointer<T, Cleanup>::QScopedPointer(const QScopedPointer<T, Cleanup>&) [with T = someAbsClass; Cleanup = QScopedPointerDeleter<someAbsClass>]' is private
 Q_DISABLE_COPY(QScopedPointer)
                ^
e3oroush
  • 3,045
  • 1
  • 17
  • 26
  • Regarding instantiating an abstract class, you should instantiate a *concrete* class, one that inherits from the abstract class. – Some programmer dude May 11 '16 at 06:12
  • 1
    Also, if you say you have "some reasons" you don't want something, you might want to elaborate. *Why* don't you want to use `QSharedPointer`? And what is the actual problem you are trying to solve by using a list of `QScopedPointer`? ([Related reading about the XY problem](http://xyproblem.info/).) – Some programmer dude May 11 '16 at 06:14
  • 1
    QScopedPointer is designed to solve another problem, but you can use std::unique_ptr which supports move semantic. – fasked May 11 '16 at 09:43
  • I think QScopedPointer is designed for membership class pointers that only valid in the scope of every object of a class or function. In my problem I read from some config file a QScopedPointer variable and pass it to a QList then destroy that variable, somehow swap ownership of the variable. Off course I could do that if my SomeClass> wasn't an abstract class. Any idea? – e3oroush May 14 '16 at 04:42
  • I think base on this [link](http://stackoverflow.com/questions/34761327/qlist-of-qscopedpointers), I simply can't use any move semantic with QScopedPointer!, So I must use another smart pointer like QSharedPointer. – e3oroush May 14 '16 at 05:19

0 Answers0