Pure virtual methods, constructors and destructors

Discussion in 'C++' started by Ruben Campos, Apr 6, 2005.

  1. Ruben Campos

    Ruben Campos Guest

    I've recently noticed that it's not allowed to call a pure (non-implemented)
    virtual method inside a constructor or a destructor, doesn't matter if this
    method is declared in the considered class itself or in one of its base
    classes. Such an attempt results in a linker undefined symbol error. Why? Is
    it right, or is it a bad issue of my compiler/linker (I'm working with
    Microsoft Visual C++ 7.1.3088)? Thank you in advance for your help.
     
    Ruben Campos, Apr 6, 2005
    #1
    1. Advertising

  2. Ruben Campos wrote:
    > I've recently noticed that it's not allowed to call a pure (non-implemented)
    > virtual method inside a constructor or a destructor, doesn't matter if this
    > method is declared in the considered class itself or in one of its base
    > classes. Such an attempt results in a linker undefined symbol error. Why? Is
    > it right, or is it a bad issue of my compiler/linker (I'm working with
    > Microsoft Visual C++ 7.1.3088)? Thank you in advance for your help.


    Calling a virtual function in a c-tor or d-tor results in static linking
    to that class' member (or the base if this doesn't have it), and not in
    a polymorphic call. That's why calling a pure virtual function from the
    c-tor or d-tor causes undefined behaviour.

    You can provide a definition of the pure virtual function just for that
    purpose:

    struct ABC {
    virtual ~ABC() = 0; // makes this class abstract
    };

    ABC::~ABC() {} // do nothing, but at least it is defined

    struct D : ABC { }; // no problem destroying it -- the ABC's dtor is
    // defined, although it is pure

    V
     
    Victor Bazarov, Apr 6, 2005
    #2
    1. Advertising

  3. Ruben Campos

    Hang Dog Guest

    Ruben Campos wrote:

    > I've recently noticed that it's not allowed to call a pure (non-implemented)
    > virtual method inside a constructor or a destructor, doesn't matter if this
    > method is declared in the considered class itself or in one of its base
    > classes. Such an attempt results in a linker undefined symbol error. Why? Is
    > it right, or is it a bad issue of my compiler/linker (I'm working with
    > Microsoft Visual C++ 7.1.3088)? Thank you in advance for your help.
    >


    What do you expect the complier to call instead of the non-implemented
    function?

    BTW a pure virtual function just says that derived classes have to
    define the method. It doesn't mean that the pure virtual can't also be
    defined, and in may cases they are.

    class A {
    public:
    virtual ~A() {};
    };
     
    Hang Dog, Apr 6, 2005
    #3
  4. Hang Dog wrote:
    > [...]
    > BTW a pure virtual function just says that derived classes have to
    > define the method. It doesn't mean that the pure virtual can't also be
    > defined, and in may cases they are.
    >
    > class A {
    > public:
    > virtual ~A() {};
    > };


    There are two things wrong about this example. First, the topic is *pure*
    virtual functions, and there are none in that example. Second, it has
    a superfluous semicolon after the body of the d-tor. Yes, I do consider
    it wrong although it's allowed by the syntax. It's a bad habit and
    reduces readability.

    Also, even if you did declare ~A() pure, you can't define it in the class:

    class A {
    public:
    virtual ~A() = 0 {} // wrong as well
    };

    It would be a syntax error. The definition has to be put at the namespace
    level.

    V
     
    Victor Bazarov, Apr 6, 2005
    #4
  5. Ruben Campos

    Ron Natalie Guest

    Ruben Campos wrote:
    > I've recently noticed that it's not allowed to call a pure (non-implemented)
    > virtual method inside a constructor or a destructor,


    Doesn't matter if it's implemented or not either. Making a virtual call
    to a pure virtual fucntion during construction/destruction is undefined
    behavior. That's the way the standard reads....it's not required to
    catch it.
     
    Ron Natalie, Apr 7, 2005
    #5
  6. Ruben Campos

    Ron Natalie Guest

    Victor Bazarov wrote:

    > Calling a virtual function in a c-tor or d-tor results in static linking
    > to that class' member (or the base if this doesn't have it), and not in
    > a polymorphic call


    Incorrect. The call may still be polymorphic, but the type of the object
    is that of the constructor that is currently running.

    Consider the following:

    class Base {
    public:
    virtual void vfunc();
    void nvfunc() {
    vfunc();
    };
    };

    class Derived : public Base {
    public:
    void vfunc();
    Derived() {
    nvfunc();
    }
    };

    class MoreDerived : public Derived {
    void vfunc();
    };

    MoreDerived foo;

    While the object is being created and the Derived function is being
    invoked, the dynmaic type is set to Derived. Derived's constructor
    calls Based::nvfunc which calls Derived::vfunc (not base::vfunc
    or MoreDerived::vfunc).

    If Base::vfunc was declared pure virtual, the behavior here is undefined.
     
    Ron Natalie, Apr 7, 2005
    #6
    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. qazmlp
    Replies:
    7
    Views:
    492
    Howard
    Jul 27, 2004
  2. heted7
    Replies:
    33
    Views:
    1,033
    Chris Dearlove
    May 12, 2005
  3. Replies:
    3
    Views:
    377
    Nitin Motgi
    Jan 31, 2006
  4. Alok
    Replies:
    5
    Views:
    4,809
    ryadav
    Apr 16, 2006
  5. Shraddha

    pure virtual constructors and distructors...

    Shraddha, Jun 25, 2007, in forum: C Programming
    Replies:
    4
    Views:
    384
    Johan Bengtsson
    Jul 6, 2007
Loading...

Share This Page