Skip to content

Latest commit

 

History

History
72 lines (59 loc) · 1.55 KB

CppQPointer.md

File metadata and controls

72 lines (59 loc) · 1.55 KB

QPointer is a Qt class to hold a pointer. Such a class is called a smart pointer, yet -in my humble opinion- QPointer behaves unexpectedly.

Prefer to use smart pointers over normal pointers [1].

#include <cassert>
#include <iostream>
#include <QPointer>

//QPointer can only hold classes derived from QObject
struct Test : public QObject
{
  Test(const int x) : m_x(x) { std::cout << "~Test\n"; }
  ~Test()
  {
    std::clog << "~Test\n";
  }
  const int m_x;
};

int main()
{
  {
    QPointer<Test> p;
    assert(!p);
    assert(!p.data());
    assert(p.isNull());
  }
  {
    QPointer<Test> p(new Test(42));
    assert(p);
    assert(p.data());
    assert(!p.isNull());
    std::clog << p->m_x << '\n';
    std::clog << "~Test will be called after this\n";
  }
  std::clog << "~Test should have been called before this\n";
}

Screen output:

~Test
42
~Test will be called after this
~Test should have been called before this

Expected screen output:

42
~Test will be called after this
~Test
~Test should have been called before this
  1. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 13: 'Ensure resources are owned by objects. Use explicit RAII and smart pointers.