Does an ABC need an implementation for a pure virtual DTOR?

This is a discussion on Does an ABC need an implementation for a pure virtual DTOR? within the C++ Programming forums, part of the General Programming Boards category; Hi, my linker is trying to look that up. So the implementation of a DTOR of an ABC can't be ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    Does an ABC need an implementation for a pure virtual DTOR?

    Hi,

    my linker is trying to look that up. So the implementation of a DTOR of an ABC can't be omitted even if the DTOR is pure virtual. Is that correct or do I suffer from some other fault?

    Thank you in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you talking about Abstract Base Class? Then it should introduce an (empty) virtual destructor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If you think about sequence of destructing any object of the derived class, you will undestand - why.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    matsp:
    Yes I mean abstract base class.

    Hm strange, here is my code:

    Code:
    // Model.h
    struct Model
    {
    	virtual ~Model() = 0;	
    	virtual const std::vector<BlasVector> wireFrame(const BlasVector& heading) const = 0;
    };
    
    
    
    // ModelFlyer.h
    class ModelFlyer : public Model
    {
    	std::vector<BlasVector> mWireFrame;
    	
    public:
    
    	ModelFlyer();
    	virtual ~ModelFlyer();	
    	virtual const std::vector<BlasVector> wireFrame(const BlasVector& heading) const;
    };
    
    
    // ModelFlyer.cpp
    #include "ModelFlyer.h"
    
    ModelFlyer::ModelFlyer()
    	:mWireFrame()
    {...}
    
    ModelFlyer::~ModelFlyer()
    {}
    
    const std::vector<BlasVector>
    ModelFlyer::wireFrame(const BlasVector& heading) const
    {...}
    And here is my linker error:
    1>ModelFlyer.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Model::~Model(void)" (??1Model@@UAE@XZ) referenced in function "public: virtual __thiscall ModelFlyer::~ModelFlyer(void)" (??1ModelFlyer@@UAE@XZ)
    1>C:\workspace-eclipse\jschlegel-dev\Win32\Debug\bin\entityDebugger.exe : fatal error LNK1120: 1 unresolved externals
    If I define Model::~Model(){} inside a Model.cpp the linker is pleased.

    Do you know what I'm doing wrong?


    vart:
    What will I understand? Why the definition is needed or why "an (empty) virtual destructor is introduced [automatically]"? The first one is understood, the second doesn't seem to happen.
    Last edited by pheres; 03-03-2008 at 07:27 AM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The destructor must always be implemented, even if it's pure.
    Code:
    class foo
    {
    public:
      virtual ~foo() = 0;
    };
    inline foo::~foo() {}
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Ok, question answered. Thank you all!

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by pheres View Post
    Hi,

    my linker is trying to look that up. So the implementation of a DTOR of an ABC can't be omitted even if the DTOR is pure virtual. Is that correct or do I suffer from some other fault?

    Thank you in advance!
    The compiler will implement one anyway, even if you specify a pure virtual destructor. There's nothing bad about it.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by brewbuck View Post
    The compiler will implement one anyway, even if you specify a pure virtual destructor. There's nothing bad about it.
    Huh? No, if you declare a pure virtual destructor, automatic generation is suppressed for this class.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    The destructor must always be implemented, even if it's pure.
    Code:
    class foo
    {
    public:
      virtual ~foo() = 0;
    };
    inline foo::~foo() {}
    Is there a reason why is has to be implemented?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's called - the base destructor is always implicitly called in the derived destructor. Therefore it has to be implemented. To have it pure, it must be explicitly declared. Therefore the compiler won't provide the implementation.
    End result: you have to implement it yourself.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by CornedBee View Post
    Huh? No, if you declare a pure virtual destructor, automatic generation is suppressed for this class.
    Are you sure? If it is required to exist regardless, why would the compiler not implement one? Allowing you to forget to implement it seems kinda stupid.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    Are you sure? If it is required to exist regardless, why would the compiler not implement one? Allowing you to forget to implement it seems kinda stupid.
    From the C++ Standard (2003) Section 12:
    The default constructor (12.1), copy constructor and copy assignment operator (12.8), and destructor (12.4) are special member functions. The implementation will implicitly declare these member functions for a class type when the program does not explicitly declare them, except as noted in 12.1.
    I guess the idea is that if you declare the destructor and forget to implement it, something might be wrong, so instead of giving a default with an empty body, the compiler simply does nothing and lets the linker complain.

    EDIT:
    I noticed that what I quoted does not talk about definition, so here are some snippets from Section 12.4:
    An implicitly-declared destructor is implicitly defined when it is used to destroy an object of its class type (3.7).
    ...
    At the point of definition of a virtual destructor (including an implicit definition
    From what I see, the former line talks about when an implicitly-declared destructor is defined, but does not talk about when an explicitly declared destructor is defined... so the implication is that explicitly declared destructors are to be explicitly defined. A virtual destructor can be implicitly defined too, when it belongs to a derived class whose base class' destructor is virtual.
    Last edited by laserlight; 03-03-2008 at 12:36 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  3. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM
  4. Information Regarding Pure Virtual Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2003, 04:43 AM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21