Why are pointer objects created while defining a class?
Unsolved
Qt WebKit
-
Hi All,
I hope that you're doing well .I've a doubt regarding the creation of pointer object of a class. I want to know why are pointer objects created in the definition of a class instead of simply creating an object of that class. -
@Swati777999 said in Why are pointer objects created while defining a class?:
I want to know why are pointer objects created in the definition of a class instead of simply creating an object of that class.
There can be multiple different reasons. Some reasons are:
- To save space. The size of a pointer is always 32 bits or 64 bits (4 bytes or 8 bytes) on today's common PCs, but the size of a class/struct depends on the sizes of all its members. If an object is too big, it cannot be created on the stack (or it becomes easier to trigger a stack overflow).
- Because some objects cannot be copied. For example, you cannot have
QList<QWidget>
; you must useQList<QWidget*>
- Because of style/convention. Qt uses pointers to show that an object is an Identity object, and it uses non-pointers to show that an object is a Value object: https://doc.qt.io/qt-6/object.html#qt-objects-identity-vs-value