2

The problem: Replace naked pointers with smart QPointers and use them with forward declared classes.

Background: As advised by virtually all texts on modern C++, one should refrain from using naked pointers and use smart pointers instead. In the Qt toolkit there is a set of smart pointers available, with one of particular interest to me in this case: QWeakPointer (Qt 4) or QPointer (Qt 5), which is supposedly freely interchangeable with a naked pointer and is set to NULL when the respective object (derived from QObject) is deleted, thus helping prevent the all too common dangling pointer problem. Yet the requirement that the object pointed to by the QPointer be of a QObject-derived type prevents usage of forward declared classes.

The question: How do I combine QPointer-type variables with forward declared classes in the class header with forward declared types?

or

How do I avoid including the whole header file for an object for which I want to use the QPointer?

Example:

#pragma once
#include <QObject>
#include <QPointer>

class MyQWidget; // Forward-declared class

class SomeClass {
   QPointer<MyQWidget> m_myWidget;
};

Note: I've read through wuestion like this one: In C++, is it possible to forward declare a class as inheriting from another class?. In my question, I'm looking for Qt-specific ways to overcome this limitation (or better design feature), if there are any.

Edit: My main compiler is MSVC 2010 and the error I get is

error C2079: 'MainWindow::m_test' uses undefined class 'QPointer<T>'
    with
    [
        T=MyQWidget
    ]
Community
  • 1
  • 1
Pavel
  • 698
  • 1
  • 11
  • 20
  • Please add a `;` after the final brace of your class definition. – fjardon Aug 21 '14 at 13:54
  • I can compile your example with Qt5 and mingw. What exactly is the message of your compiler ? – fjardon Aug 21 '14 at 13:58
  • @fjardon: On MSVC 2010 I get: error C2079: 'MainWindow::m_test' uses undefined class 'QPointer' with [ T=MyQWidget ] – Pavel Aug 21 '14 at 14:25
  • 1
    Make sure you have a constructor and destructor in your c++ file for your class. I assume from your error this is MainWindow.cpp. Also make sure that you also include the header for MyQtWidget in that cpp file. Although I usually use std::unique_ptr now instead of the any of the Qt pointers. – drescherjm Aug 21 '14 at 14:31
  • That is not the point which should be adressed here. The question is: why does the forward declaration not work? Cold you please share the declaration of MainWindow. Normally it should work as in your example above. – KimKulling Aug 21 '14 at 14:44
  • @drescherjm: I double checked constructors and destructors and even inclusion of the .h files, they are all present and I still get the error. I need the NULL on delete that comes with the QPointer, so I need a std:: pointer is not optimal here. – Pavel Aug 21 '14 at 14:48
  • Are you sure you have included correctly? This seems to be the compiler not able to find QPointer instead of MyQWidget. – fxam Aug 21 '14 at 14:53
  • @fxam: I haven't, that was it. I'm on my way here to post all source code (as per comment from KimKulling) when I noticed my omission (I included the in a wrong file). Want to turn your comments to answer? Maybe it will be helpful for other lost soul as well... – Pavel Aug 21 '14 at 14:57
  • 1
    You gave up too early, look: https://bugreports.qt.io/browse/QTBUG-29588 (the question is absolutely correct, that is, only missing a compact self-contained source set to build) – mlvljr Jan 10 '15 at 22:43

1 Answers1

6

Are you sure you have included QPointer correctly? This seems to be like the compiler not able to find QPointer instead of MyQWidget.

fxam
  • 3,874
  • 1
  • 22
  • 32
  • 5
    Wait what? Thats the right answer? QPointer is included in line three. Could the OP please clear up what was the problem, and how including QPointer "correctly" changed it. – Paul Aug 21 '14 at 15:25
  • I expect the example code did not match the actual code. Kind of hard to help in this case. – drescherjm Aug 22 '14 at 14:16
  • 1
    A -1 and a W-T-F indeed :) – mlvljr Jan 10 '15 at 22:40