Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:47:06+00:00 2026-06-14T12:47:06+00:00

I have a question that may seem too simple but I’d like to understand

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:47:08+00:00Added an answer on June 14, 2026 at 12:47 pm

    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:

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        Parent one;
        ChildA two;
        ChildB three;
        // This will immediately terminate the application
        QTimer::singleShot(0, &a, SLOT(quit()));
        return a.exec();
    }
    

    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

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        // Force scope
        {
            Parent one;
            ChildA two;
            ChildB three;
        }
    
        return a.exec();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry that this may seem like a rookie question, but it's a real pain
This may seem like an odd question, but I have my own reasons for
Okay this may seem too simple of a question but I've wasted enough time
I have a question that may be quite naive, but I feel the need
I have a question that may sound odd, but being somewhat of a newbie,
this question may seem too basic to some, but please bear with be, it's
this may seem like a stupid question, but it is stumping me nontheless. I'm
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
Have a question that I can't seem to find an answer for. I am
This may be a question that others have seen, but I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.