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

  • SEARCH
  • Home
  • 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 6865619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:04:35+00:00 2026-05-27T03:04:35+00:00

#define CLASS(ID) class ID{ \ public: \ ID(int) { cout<<#ID <<\tconstructor<<endl; } \ ~ID(){cout<<#ID<<\tDestroyed

  • 0
#define CLASS(ID) class ID{ \
    public: \
            ID(int) { cout<<#ID <<"\tconstructor"<<endl; } \
           ~ID(){cout<<#ID<<"\tDestroyed  "<<endl;} \
};
CLASS(Base);
CLASS(Member);

class Derived : public Base {
    public:
        Member *mem;
        Derived(int x) : Base(1) {
            cout<<"Derived constructor"<<endl;
            mem=new Member(2);
        }
        ~Derived()
        {
            cout<<"Derived Destroyed"<<endl;
            delete mem;
        }
};

int main(int argc, char** argv) {
    Derived * der=new Derived(1);
    cout<<"****"<<endl;
    delete der;
}

The output for this is:

Derived constructor
Member  constructor
****
Derived Destroyed
Member  Destroyed  
Base    Destroyed

In the second version :

class Derived : public Base {
    public:
        Member *mem;
        Derived(int x) : Base(1) {
            cout<<"Derived constructor"<<endl;
        }

        ~Derived()
        {
            cout<<"Derived Destroyed"<<endl;
            delete mem;
        }
};

Why in the first version the Base constructor don’t executes when an instance from the same class has been instantiated?

  • 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-05-27T03:04:36+00:00Added an answer on May 27, 2026 at 3:04 am

    On GCC version 4.6 I got this

    Base    constructor
    Derived constructor
    Member  constructor
    ****
    Derived Destroyed
    Member  Destroyed  
    Base    Destroyed  
    

    So it seems to be working

    On a side note your base destructor should be virtual

     virtual ~ID(){}
    

    this way when you only have a reference to the base you derived will still cleanly be removed.

    On a side note, please don’t use defines like this unless you have too, also wrapping up mem in a smart point is also a good idea. Like auto_ptr in c++98 (ideally the boost ptrs) or unique_ptr in c++11.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int
I have a class defined as: class Obj { public: int width, height; Obj(int
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
Is it good practice to let abstract classes define instance variables? public abstract class
I have the below exception class. class ExceptionTest : std::exception { public: ExceptionTest(int value):
I am trying to call a constructor of a Base Class in a function
I have a class defined as public class viewGroups extends ListActivity Somewhere in the
I defined next class with virtual properties: public class Order: BaseEPharmObject { public Order()
I've got a simple class defined as: public class MyClass { //Some properties public

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.