QMap of QScopedPointer
-
Hello
Can i create a QMap of QScopedPointer?
For example:QMap<int, QScopedPointer<MyClass>> myClassObjects; for (int i = 1; i <= 10; i++) { myClassObjects.insert(i, QScopedPointer<MyClass>(new MyClass())); }
when i make so, i have compile error. What is the right way to do this?
-
Hi,
No you can't. QScopePointer cannot be copied.
Why do you want such a map ?
-
@SGaist
For example: i have modbus port and need modbus device. I use QScopedPointer fot it.QScopedPointer<QModbusRtuSerialMaster> modbusDevice;
But now I want to use some ports, so I need N devises. I decided to store them in QMap, but as i see, it is wrong way)
-
@Pavel-Romankov
Since it descends from QObject, no need to do more then that:auto device=new QModbusRtuSerialMaster(this);
-
@Pavel-Romankov said in QMap of QScopedPointer:
I use my own class, not descended from QObject, and store it in context which not QObject too.
Why not ?
Anyway, to delete all the container you can use:
qDeleteAll(map.begin(), map.end()); -
You could also use a copyable smart pointer like QSharedPointer if threading issues are considered.