1

Sorry about the wierd title, feel free to come up with a better one if you can think of one.

Here is my issue.

I have a structure that looks something like this:

QObject -> MyBase -> MyDerived

I then have a function that will take any of the derived types as a QPointer;

void function(QPointer<MyBase> base) {
   ...
}

The issue is now that I cannot pass QPointer<MyDerived> into this function, which I would if I used standard pointers. Do I have to cast this object to make it fit to the function? Or should I just use normal pointers?

Also, does anyone know the standard behavior when you allocate an object which fails? Should you always do this in a try-block or wrap it in something to check if it's null or not? I read the Qt documentation and they said that most of their classes returned a null value if it failed to allocate. When is that statement true and what can I do if I don't want to use try-catch?

demonplus
  • 5,613
  • 12
  • 49
  • 68
chikuba
  • 4,229
  • 6
  • 43
  • 75

2 Answers2

7

you should use normal pointer. This is what Qt Document says about using QPoiner

"A guarded pointer will automatically cast to a T *, so you can freely mix guarded and unguarded pointers. This means that if you have a QPointer<QWidget>, you can pass it to a function that requires a QWidget *. For this reason, it is of little value to declare functions to take a QPointer as a parameter; *just use normal pointers*. Use a QPointer when you are storing a pointer over time."

Kunal
  • 3,475
  • 21
  • 14
  • but how can I in the function protect myself from dangeling pointers/ – chikuba Mar 16 '12 at 03:58
  • that function does not own the pointer right ? someone else own it, so it don't need to delete it, am I missing something ? – Kunal Mar 16 '12 at 04:00
  • the function will become the new owner. just wondered what will happen if the object gets deleted when the function points to it. i should propaly just read up more about guarded pointers – chikuba Mar 16 '12 at 04:04
  • i think QPointer are not good when you want to pass the ownership, Qt docs says that "Guarded pointers are useful whenever you need to store a pointer to a QObject that is owned by someone else" – Kunal Mar 16 '12 at 04:11
0

As @Kunal mentioned, it's better to pass raw pointer in your case, but I see that you are experiencing pointer ownership problems. Passing raw pointers to object's methods is dangerous, because when your code base grows, you'll soon become confused about which object is the owner of that particular pointer and this can lead to memory leaks very easily. For that reason, I would recommend to use QSharedPointer instead of QPointer or even raw pointer. QSharedPointer supports polymorphism, so you will be able to pass it (as a value) to any function/object you would like, without worrying about ownership, because QSharedPointer will take ownership of the pointer and will delete it when all last instance of QSharedPointer goes out of scope.

Davita
  • 8,928
  • 14
  • 67
  • 119