-2

How does it work?

QDesktopWidget *Desktop=NULL;
auto Desktop_Rect=Desktop->screenGeometry();
pDebug()<<Desktop_Rect.width()<<","<<Desktop_Rect.height(); //1440,900

If I rewrite it as following:

auto Desktop_Rect=QDesktopWidget::screenGeometry();//Error:screenGeometry is not a static method.
pDebug()<<Desktop_Rect.width()<<","<<Desktop_Rect.height();
Nya
  • 310
  • 2
  • 10
  • What does it print in the first case? I think, if it works - it works only by a lucky chance. The second error - compile time error. Pretty reasonable. – vahancho Jan 19 '15 at 15:01
  • In the first case, it always prints "1440,900".Otherwise, I won't open a question. BTW, I'm using Qt Creator – Nya Jan 19 '15 at 15:17
  • `QDesktopWidget()->screenGeometry().height();` – Dmitry Sazonov Jan 19 '15 at 17:15

2 Answers2

1

Basically your question has less to do with Qt but with C++.

The pointer isn't required to call the method, but the type of the pointer.

The method might not use this, so it runs the code just fine.

The bahaviour should be undefined, depends on the implementation of screenGeometry method.

P.S. Compile with warnings AND DON'T IGNORE THEM (suggested by Jeoren in comments)

deimus
  • 9,565
  • 12
  • 63
  • 107
  • This function returns a QRect, describing the geometry of the wiget. This most definitely accesses instance data, so my guess is the first snippet will crash. – Jeroen Jan 19 '15 at 15:14
  • agree, definitely something should go wrong. – deimus Jan 19 '15 at 15:15
  • Plus one on the "compile with warning" comment, and I would add "AND DON'T IGNORE THEM" – Jeroen Jan 19 '15 at 15:17
  • @Jeroen While what you say is true in general, [it won't save you here](http://coliru.stacked-crooked.com/a/d8a787ac7fd711de). – Baum mit Augen Jan 19 '15 at 15:23
1

This works in all cases where you call a class method on a NULL-pointer. The compiler does not (cannot) check for valid values of the pointer at compile time. It only looks at the type of the pointer, and as long as that checks out (in this case, the QDesktopWidget class does indeed have a function called screengGeometry that accepts 0 parameters) it will happily compile your code.

However, at runtime, this will most likely crash as soon as the code in that function accesses the class's data members. That is when it tries to dereference the "this" pointer, which in this case is NULL. The fact that your first example runs successfully simply means it does not access instance data, but only static or global data.

The second snippet is simply a completely different case, where you are trying to call a method as if it was static, while it is not. This results in the entirely correct and helpful error message you quote.

Ask yourself this though: What should the function return when you call screenGeometry when there is no instance to which it belongs. Both code snippets try to do something nonsensical.

Jeroen
  • 803
  • 1
  • 7
  • 13
  • In fact, the first one always works fine.And that's confuses what exactly me. – Nya Jan 19 '15 at 15:25
  • That means the data screenGeometry() returns is static, but the function itself is not. [Like so](http://coliru.stacked-crooked.com/a/835c72f77579f3a4) – Jeroen Jan 21 '15 at 14:33