Q:

downcasting in Qt

could someone please tell me why the dynamic_cast in the following code (five lines from the bottom) fails? I'm afraid it's something obvious, but I can't see it.
//dynamic_cast.h
#ifndef DYNAMIC_CAST_H
#define DYNAMIC_CAST_H
#include <QObject>
class Parent: public QObject
{
 Q_OBJECT
 public:
 Parent(QObject * parent = 0) {}
};
class Child: public QObject
{
 Q_OBJECT
 public:
 Child(QObject * parent = 0) {}
};
#endif // DYNAMIC_CAST_H

//dynamic_cast.cpp
#include <iostream>
#include "dynamic_cast.h"
using namespace std;
int main (int argc, char *argv[])
{
  Parent * aParent = new Parent;
  Child * aChild = new Child(aParent);
  Parent * anotherParent = dynamic_cast <Parent *>(aChild->parent());
  if (anotherParent==0)
      cout << "Assigned null pointer" << endl;
  else cout <<"No problem!";
  return 0;
}

A:

Child(QObject * parent = 0) {}

You're not doing anything with the parent pointer - you're just throwing it away. You should pass the pointer to QObject's constructor like this:
Child(QObject * parent = 0) : QObject(parent)
{}

Without that the default constructor of QObject will be called and the parent argument ignored.

