Search code examples
c++virtual-functionslifetimevirtual-destructor

C++ Virtual Inheritance


#include <stdio.h>


class abc{
    public:
        abc     *next;

    protected:
        int             flags;
        const char *    name;
        const char *    comments;

    public:
        abc(const char *name, const char *comments, int flags);

        virtual ~abc() {
            printf("\nReached at virtual ~abc\n");
            printf("Returning from at virtual ~abc\n");
        }   
};

class def: public abc{
    public:
        def (const char *myname, const char *mycomments, 
              int myflags): abc(myname, mycomments, myflags)
        {
            printf("\nreached at def\n");
            printf("name=%s; comments=%s\n", myname, mycomments);
            printf("Returning from def\n");
        }
};

class ghi: public def{
    public:
        ghi(const char *myname2, const char *mycomments2, 
            int myflags2): def(myname2, mycomments2, myflags2)
        {
            printf("\nreached at ghi\n");
            printf("name=%s; comments=%s\n", myname2, mycomments2);
            printf("Returning from ghi\n");
        }
};

class jkl: public def{
    public:
        jkl(const char *myname2, const char *mycomments2, 
            int myflags2): def(myname2, mycomments2, myflags2)
        {
            printf("\nreached at ghi\n");
            printf("name=%s; comments=%s\n", myname2, mycomments2);
            printf("Returning from ghi\n");
        }
};

ghi myVar("myVar", "Testing it", 0);
jkl myVar2("myVar2", "Testing it Again", 0);

abc::abc(const char *name, const char *comments, int flags) : next(0){
    printf("\nreached at abc::abc\n");
    printf("name=%s; comments=%s\n", name, comments);
    printf("Returning from abc:abc\n");
}



int main(void){
    printf("\nrunning main function\n");
    printf("ending main function\n");
    return 0;
}

The Output:

reached at abc::abc
name=myVar; comments=Testing it
Returning from abc:abc

reached at def
name=myVar; comments=Testing it
Returning from def

reached at ghi
name=myVar; comments=Testing it
Returning from ghi

reached at abc::abc
name=myVar2; comments=Testing it Again
Returning from abc:abc

reached at def
name=myVar2; comments=Testing it Again
Returning from def

reached at ghi
name=myVar2; comments=Testing it Again
Returning from ghi

running main function
ending main function

Reached at virtual ~abc
Returning from at virtual ~abc

Reached at virtual ~abc
Returning from at virtual ~abc

I don't understand:

  • how is the function virtual ~abc called?
  • Why it is running at the end of program?
  • What is the purpose of function?

Solution

  • ~abc is the destructor of abc. It's a function that's there to destroy an object when it isn't needed anymore.

    At the end of the program every object is destroyed, for that the program calls every destructor. You defined your destructor to print those lines, so it does.

    Also, every object in the tree inherits from abc, so they all inherit the destructor. You declare two object, ghi and jkl. They have to be destroyed at the end of the program, so they call the destructor they inherited from abc.

    Destructors are used to destroy objects in custom ways so that no memory is leaked. For example, if your object has a pointer to something else in it, you would set that pointer to nullptr for safety.

    Note that every object has a default destructor, but it doesn't print anything by default, that's why you didn't know they existed.