About virtual destructor

Discussion in 'C++' started by sam, Jan 19, 2007.

  1. sam

    sam Guest

    Hi,
    See when i reading a sourcecode of a program,
    I read that the constructor is ordinary and after that the programmer
    has written virtual destructor for that constructor .
    Why we use the virtual destructor whats the use of it?
    the code is like this:

    Network(int input,int output);
    Network(&Network);
    virtual ~Network();

    My question is why he use virtual destructor.
    sam, Jan 19, 2007
    #1
    1. Advertising

  2. sam

    kwikius Guest

    sam wrote:
    > Hi,
    > See when i reading a sourcecode of a program,
    > I read that the constructor is ordinary and after that the programmer
    > has written virtual destructor for that constructor .
    > Why we use the virtual destructor whats the use of it?
    > the code is like this:
    >
    > Network(int input,int output);
    > Network(&Network);
    > virtual ~Network();
    >
    > My question is why he use virtual destructor.


    1) Pointer to dynamically allocated class derived from Network can be
    correctly deleted via a Network*
    2) Network* can be used for dynamic_cast.

    regards
    Andy Little
    kwikius, Jan 19, 2007
    #2
    1. Advertising

  3. sam

    kwikius Guest

    kwikius wrote:
    > sam wrote:
    > > Hi,
    > > See when i reading a sourcecode of a program,
    > > I read that the constructor is ordinary and after that the programmer
    > > has written virtual destructor for that constructor .
    > > Why we use the virtual destructor whats the use of it?
    > > the code is like this:
    > >
    > > Network(int input,int output);
    > > Network(&Network);
    > > virtual ~Network();
    > >
    > > My question is why he use virtual destructor.

    >
    > 1) Pointer to dynamically allocated class derived from Network can be
    > correctly deleted via a Network*


    And as important correct destructor for derived from Newtwork will be
    called, so any derived resources will be cleaned up.

    regards
    Andy Little
    kwikius, Jan 19, 2007
    #3
  4. sam

    Jim Langston Guest

    "sam" <> wrote in message
    news:...
    > Hi,
    > See when i reading a sourcecode of a program,
    > I read that the constructor is ordinary and after that the programmer
    > has written virtual destructor for that constructor .
    > Why we use the virtual destructor whats the use of it?
    > the code is like this:
    >
    > Network(int input,int output);
    > Network(&Network);
    > virtual ~Network();
    >
    > My question is why he use virtual destructor.


    If you derive from a base class and don't have a virtual destructor, there
    are times when the derived classe's destructor won't be called.

    Case in point, output from this program is:

    Base1 Destructor
    Derived2 Destructor
    Base2 Destructor

    We are missing a Derived2 Destructor

    #include <iostream>
    #include <string>

    class Base1
    {
    public:
    ~Base1() { std::cout << "Base1 Destructor\n"; }
    };

    class Derived1: public Base1
    {
    public:
    ~Derived1() { std::cout << "Derived1 Destructor\n"; }
    };

    class Base2
    {
    public:
    virtual ~Base2() { std::cout << "Base2 Destructor\n"; }
    };

    class Derived2 : public Base2
    {
    public:
    ~Derived2() { std::cout << "Derived2 Destructor\n"; }
    };

    int main ()
    {
    Base1* Foo = new Derived1;
    Base2* Bar = new Derived2;

    // delete Foo does not cause Derived1's destructor
    // to be called, because Base1's destructor is not virtual
    delete Foo;

    delete Bar;

    std::string wait;
    std::getline( std::cin, wait );
    }
    Jim Langston, Jan 19, 2007
    #4
  5. sam

    hit_pc Guest

    On Fri, 19 Jan 2007 20:46:20 +0800, sam <> wrote:

    > Network(int input,int output);
    > Network(&Network);
    > virtual ~Network();
    >
    > My question is why he use virtual destructor.
    >

    if :
    class A:public Network{
    //new some memory
    }
    -------
    Network *network=new A();
    delete network;//here will run the ~Network() not the ~A() if you didn't
    add virtual before ~Network;
    ---------------------



    --
    Hello,World!
    ----legolaskiss.
    hit_pc, Jan 19, 2007
    #5
  6. sam

    Jim Langston Guest

    "Jim Langston" <> wrote in message
    news:zf3sh.133$...
    > "sam" <> wrote in message
    > news:...
    >> Hi,
    >> See when i reading a sourcecode of a program,
    >> I read that the constructor is ordinary and after that the programmer
    >> has written virtual destructor for that constructor .
    >> Why we use the virtual destructor whats the use of it?
    >> the code is like this:
    >>
    >> Network(int input,int output);
    >> Network(&Network);
    >> virtual ~Network();
    >>
    >> My question is why he use virtual destructor.

    >
    > If you derive from a base class and don't have a virtual destructor, there
    > are times when the derived classe's destructor won't be called.
    >
    > Case in point, output from this program is:
    >
    > Base1 Destructor
    > Derived2 Destructor
    > Base2 Destructor
    >
    > We are missing a Derived2 Destructor


    Correction, we are missing a Derived1 Destructor

    > #include <iostream>
    > #include <string>
    >
    > class Base1
    > {
    > public:
    > ~Base1() { std::cout << "Base1 Destructor\n"; }
    > };
    >
    > class Derived1: public Base1
    > {
    > public:
    > ~Derived1() { std::cout << "Derived1 Destructor\n"; }
    > };
    >
    > class Base2
    > {
    > public:
    > virtual ~Base2() { std::cout << "Base2 Destructor\n"; }
    > };
    >
    > class Derived2 : public Base2
    > {
    > public:
    > ~Derived2() { std::cout << "Derived2 Destructor\n"; }
    > };
    >
    > int main ()
    > {
    > Base1* Foo = new Derived1;
    > Base2* Bar = new Derived2;
    >
    > // delete Foo does not cause Derived1's destructor
    > // to be called, because Base1's destructor is not virtual
    > delete Foo;
    >
    > delete Bar;
    >
    > std::string wait;
    > std::getline( std::cin, wait );
    > }
    >
    >
    Jim Langston, Jan 19, 2007
    #6
  7. sam

    Pete Becker Guest

    Jim Langston wrote:
    >
    > If you derive from a base class and don't have a virtual destructor, there
    > are times when the derived classe's destructor won't be called.
    >
    > Case in point, output from this program is:
    >
    > Base1 Destructor
    > Derived2 Destructor
    > Base2 Destructor
    >


    Only by accident, however. Formally, the behavior is undefined.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Jan 19, 2007
    #7
  8. hit_pc wrote:

    > sam wrote:
    >
    >> Network(int input,int output);
    >> Network(&Network);
    >> virtual ~Network();
    >>
    >> My question is why he use virtual destructor.
    >>

    > if :
    > class A:public Network{
    > //new some memory
    > }
    > -------
    > Network *network=new A();
    > delete network;//here will run the ~Network() not the ~A() if you didn't
    > add virtual before ~Network;
    > ---------------------


    That's just silly.

    You should write:

    A* pNetwork = new A();
    delete pNetwork;


    The virtual constructor implies a "virtual constructor"
    (or better, a factory) so that you can write subprograms
    which allow you to create and destroy copies of objects
    passed by reference without knowing the actual type of the object.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    E. Robert Tisdale, Jan 20, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Calvin Lai
    Replies:
    7
    Views:
    546
    Calvin Lai
    Dec 18, 2003
  2. Chunhui Han
    Replies:
    2
    Views:
    492
  3. frs
    Replies:
    20
    Views:
    736
    Alf P. Steinbach
    Sep 21, 2005
  4. arun
    Replies:
    2
    Views:
    534
    benben
    Jun 13, 2006
  5. Jimmy Hartzell
    Replies:
    0
    Views:
    411
    Jimmy Hartzell
    May 19, 2008
Loading...

Share This Page