PDA

View Full Version : Why have they put the constructor of QObject and QLayout "PRIVATE"????



joovoo
2nd July 2017, 10:48
Hello people! Don't you see that the fact that constructors of some object like QObject,QLayout are private make that can't use QVector for using loops on sub class of QObject which will shorcut the code? I mean for example :

QVector <QWidget> widow (100);
QVBoxLayout *layout=new QVBoxLayout;
for (i=o;i <100;i++)layout->addWidget (&widow [i]);

Ginsengelf
3rd July 2017, 07:59
Hi, you can do that by using a vector of pointers.

Ginsengelf

d_stranz
11th July 2017, 20:06
The copy constructor and assignment operator are private, not the basic constructor. These are private to prevent copying. QObject-based class instances have a parent-child hierarchy; that is, an instance of a QObject class has a parent and itself can have children. If assignment and copying were allowed, this would mean that an instance could have multiple parents (or that different parents could have the same children). Parents own and control the lifetimes of their children, so a case where a child had more than one parent would not work.

As Ginsengelf says, nothing prevents you from assigning and copying pointers to QObject instances. This is exactly how the parent-child relationship is implemented - the parent holds pointers to its children, and a child holds a pointer to its parent.