Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How do I copy an Object (Object Pointer) to another?
dcbasso last edited by
I have an object and I want to make a copy of them.
The that that I only have the pointer of this object, as see below:
MyObject* myObj = new MyObject();
I need to make a copy of this object, I need to have two objects but in different pointer reference... I don't need to link one to another, I need a copy.
How can I make that?
Sam last edited by
You can not copy QObjects. See https://qt-project.org/doc/qt-4.8/object.html#identity-vs-value .
MuldeR last edited by
In C++ you would create a copy of an object by using the copy constructor.
With local objects:
With pointers to objects:
@MyObject *obj1 = new MyObject();
MyObject *obj2 = new MyObject(*obj1);@
Note that if your class "MyObject" has pointer member variables, you may need to implement your own copy constructor (instead of the implicit one) in order to ensure the desired "deep copy" behavior.
But as already mentioned:
[quote]QObject and all subclasses of QObject (direct or indirect) have their copy constructor and assignment operator disabled![/quote]