Does destructor of derived class remove virtual table?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Koller

    Does destructor of derived class remove virtual table?

    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
  • Rolf Magnus

    #2
    Re: Does destructor of derived class remove virtual table?

    Martin Koller wrote:
    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > Interesting for me is also, that I get the same error if I call
    > b->isA() in the constructor of Base.[/color]

    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.
    [color=blue]
    > 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)[/color]

    It is correct.

    Comment

    • David Harmon

      #3
      Re: Does destructor of derived class remove virtual table?

      On Sat, 21 Feb 2004 17:03:32 +0100 in comp.lang.c++, Martin Koller
      <m.koller@surfe u.at> was alleged to have written:[color=blue]
      >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,[/color]

      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:



      Comment

      • Martin Koller

        #4
        Re: Does destructor of derived class remove virtual table?

        [color=blue]
        > It is correct.[/color]

        Thanks a lot for all replies.

        Martin

        Comment

        Working...