PDA

View Full Version : QPointer gives compilation errors.



pkj
6th September 2011, 08:04
I have class B inheriting Class A which in turn inherits QObject.


class A: public QObject{
Q_OBJECT
...
};
class B: public A{
Q_OBJECT
...
};

At some place I instantiate class B. At some other place I want to use Class B pointer. To avoid dangling pointer, QPointer looked very interesting. So while storing the pointer of class B, I used QPointer<B> bptr
But bptr = bClassInstancePtr gives compilation errors like cannot convert from B* to QObject* . What am I doing wrong?

UASlaX
6th September 2011, 11:37
What is type of bClassInstancePtr? Can you give a complete example with creation and assign pointer?

pkj
6th September 2011, 12:44
Let me explain it again. I intend to keep a pointer of class B in some class X. I have instantiated class B(new'ed class B) in class Y. From there i wish to pass the pointer of new'ed class B to class X. To avoid worrying about dangling pointer, instead of having B* bptr, I have QPointer<B> bptr.


class A: public QObject{
Q_OBJECT
...
};
class B: public A{
Q_OBJECT
...
};

class Y{
Y(X* px){
x=px;
bptr = new B();
someMethodToAssignInX()

}
private:
void someMethodToAssignInX(){x->bptr = bptr;}
B* bptr;
X* x;
}
class X{
public:
QPointer<B> bptr;
}


Basically I want to use Qpointer to hold my pointer reference to class so that I can reap QPointer's benefits. I am not able to do so because of compilation errors.

UASlaX
6th September 2011, 12:56
Can you write what exactly writes the compiler? Maybe you didn't write include of class B in assign place?

pkj
6th September 2011, 13:11
Compiler doesn't know how to convert from B* to QObject* . (!= No conversion from B* to QObject*)
And why should i need to include the file with class B in the file where i write class X? I don't dereference B ptr ever. All i do is store the pointer. Why shouldn't a forward declaration do?

wysota
6th September 2011, 14:29
What am I doing wrong?

Add #include "b.h" to your code where you do the assignment.

UASlaX
6th September 2011, 14:42
And why should i need to include the file with class B in the file where i write class X?
QPointer uses mechanisms of QObject. He needed convert your type to QObject. I think, QPointer uses destroyed() signal from QObject.

pkj
6th September 2011, 15:13
#include "b.h" did the trick. Thanks. What I don't get, however, was why was it necessary. Going through qpointer code, I saw they store it as QObject*. Now QObject was being include'd. QPointer class is only interested in the QObject part of the class B. I think it should have gone through??

UASlaX
6th September 2011, 15:42
#include "b.h" did the trick. Thanks. What I don't get, however, was why was it necessary. Going through qpointer code, I saw they store it as QObject*. Now QObject was being include'd. QPointer class is only interested in the QObject part of the class B. I think it should have gone through??
As the compiler know that B is derived from QObject? You give to QPointer type B and QPointer converts them to QObject. For this conversion is necessary to know full specification of the type.

wysota
7th September 2011, 08:18
QPointer uses mechanisms of QObject. He needed convert your type to QObject. I think, QPointer uses destroyed() signal from QObject.
No, QPointer doesn't use destroyed(), there is a dedicated mechanism embedded in QObject for handling QPointer.


What I don't get, however, was why was it necessary.
Somewhere in your code you have the following line:

class B;
which informs the compiler (forward declares the B class) that there exists a class called "B". It lets you use pointers to B in your code (as the size of pointer is fixed) without a full declaration of the B class (which is needed to use objects and methods as the compiler needs to know the size of the object and offset of methods in the object). But it doesn't inform the compiler that B is derived from QObject. Without that knowledge the compiler cannot allow a cast from B to QObject.