Does destructor of derived class remove virtual table?

Discussion in 'C++' started by Martin Koller, Feb 21, 2004.

  1. Hi list,

    I have a small example which gives me "pure virtual function called".

    I'm calling a virtual function in the destructor of the base class, where
    the pointer used is effectively pointing to a derived class, but the whole
    thing is in the process of being destroyed.

    Interesting for me is also, that I get the same error if I call b->isA() in
    the constructor of Base.

    I just wanted to know, if this behavior is correct in C++ terms or is it a
    compiler issue ? (using gcc-2.3.2 on Linux)

    (Please CC me on mail)

    #include <iostream>

    class Base
    {
    public:
    Base(Base *x) { b = x; };
    virtual ~Base()
    {
    b->isA();
    };

    virtual int isA() = 0;

    private:
    Base *b;
    };

    class Derived : public Base
    {
    public:
    Derived() : Base(this) { std::cerr << isA() << std::endl; };

    virtual int isA() { return 1; }
    };


    main()
    {
    Derived *d = new Derived();

    delete d;
    }

    Thanks,

    Martin
    Martin Koller, Feb 21, 2004
    #1
    1. Advertising

  2. Martin Koller

    Rolf Magnus Guest

    Martin Koller wrote:

    > Hi list,
    >
    > I have a small example which gives me "pure virtual function called".
    >
    > I'm calling a virtual function in the destructor of the base class,
    > where the pointer used is effectively pointing to a derived class, but
    > the whole thing is in the process of being destroyed.


    In the destructor of the base class, the derived part of the objects is
    already destroyed, so it isn't anymore an instance of the derived
    class.

    > Interesting for me is also, that I get the same error if I call
    > b->isA() in the constructor of Base.


    Same principle. In the base class constructor, the derived part of the
    object does not yet exist. Therefore a polymorphic call won't call the
    derived implementation of the virtual function.

    > I just wanted to know, if this behavior is correct in C++ terms or is
    > it a compiler issue ? (using gcc-2.3.2 on Linux)


    It is correct.
    Rolf Magnus, Feb 21, 2004
    #2
    1. Advertising

  3. Martin Koller

    David Harmon Guest

    On Sat, 21 Feb 2004 17:03:32 +0100 in comp.lang.c++, Martin Koller
    <> was alleged to have written:
    >I have a small example which gives me "pure virtual function called".
    >
    >I'm calling a virtual function in the destructor of the base class,


    See also the topic "[23.3] When my base class's constructor calls a
    virtual function on its this object, why doesn't my derived class's
    override of that virtual function get invoked?" in Marshall Cline's C++
    FAQ. The remarks regarding constructors mostly apply for the same
    reasons to destructors. You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/
    David Harmon, Feb 21, 2004
    #3

  4. > It is correct.


    Thanks a lot for all replies.

    Martin
    Martin Koller, Feb 22, 2004
    #4
    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. Chunhui Han
    Replies:
    2
    Views:
    492
  2. qazmlp
    Replies:
    1
    Views:
    556
    qazmlp
    Apr 10, 2005
  3. Replies:
    1
    Views:
    386
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    374
    Victor Bazarov
    May 23, 2007
  5. pritesh kadam
    Replies:
    1
    Views:
    419
    Victor Bazarov
    Jul 13, 2012
Loading...

Share This Page