I have a question that may seem too simple but I’d like to understand the answer to anyway.
Example code bellow.
#include <QtCore/QCoreApplication>
#include "parent.h"
#include "childa.h"
#include "childb.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Parent one;
ChildA two;
ChildB three;
return a.exec();
}
#ifndef CHILDA_H
#define CHILDA_H
#include <iostream>
#include "parent.h"
using namespace std;
class ChildA : public Parent
{
public:
ChildA();
~ChildA();
};
#endif // CHILDA_H
#ifndef CHILDB_H
#define CHILDB_H
#include <iostream>
#include "parent.h"
using namespace std;
class ChildB : public Parent
{
public:
ChildB();
~ChildB();
};
#endif // CHILDB_H
#ifndef PARENT_H
#define PARENT_H
#include <iostream>
using namespace std;
class Parent
{
public:
Parent();
virtual ~Parent();
};
#endif // PARENT_H
#include "childa.h"
ChildA::ChildA()
{
cout << "in Child A const\n";
}
ChildA::~ChildA()
{
cout << "in Child A destructor\n";
}
#include "childb.h"
ChildB::ChildB()
{
cout << "in Child B const\n";
}
ChildB::~ChildB()
{
cout << "in Child B destructor\n";
}
#include "parent.h"
Parent::Parent()
{
cout << "in Parent const\n";
}
Parent::~Parent()
{
cout << "in Parent destructor\n";
}
Why is it that I don’t see the destructors called?
The object variables should go out of scope in main and the destructors should be called, no?
Your objects never go out of scope because your application doesn’t exist (i.e. you don’t go out of scope of the main function). You need to close your application and you do it like so:
Note that you can set the timer to execute in a specific amount of time, for the example above I’ve set it to execute after 0 ms.
If you don’t want to close the application, then you can force the scope