Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream'

Discussion in 'C++' started by tomek, Nov 27, 2003.

  1. tomek

    tomek Guest

    Can anybody explain why can I not get rid of the above message
    in the following class definition? [I am using Borland C++ Builder 5.0]
    Thank you in advance.


    class BasicMidReader : public TMemoryStream
    {
    //some private date
    public:
    BasicMidReader(char* FN)
    {// some code}
    ~BasicMidReader() {// some code}
    };
    tomek, Nov 27, 2003
    #1
    1. Advertising

  2. tomek

    David Fisher Guest

    "tomek" <> wrote:

    > Can anybody explain why can I not get rid of the above message
    > in the following class definition? [I am using Borland C++ Builder 5.0]

    ....
    > class BasicMidReader : public TMemoryStream
    > {
    > //some private date
    > public:
    > BasicMidReader(char* FN)
    > {// some code}
    > ~BasicMidReader() {// some code}
    > };


    I haven't used Borland C++, but just a guess:

    "virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base
    class" sounds like TMemoryStream could have a virtual destructor, and you
    haven't declared ~BasicMidReader() as virtual. If so, I would expect this to
    be a warning rather than an error, since inherited virtual functions are
    also virtual - even if they don't say so (but they should !)

    (BTW: in general, all destructors should be virtual unless you are sure the
    class will never be used as a base class - this even applies if the
    destructor is empty (ie. there is nothing to explicitly delete)).

    David F
    David Fisher, Nov 29, 2003
    #2
    1. Advertising

  3. "David Fisher" <> wrote...
    > "tomek" <> wrote:
    >
    > > Can anybody explain why can I not get rid of the above message
    > > in the following class definition? [I am using Borland C++ Builder 5.0]

    > ...
    > > class BasicMidReader : public TMemoryStream
    > > {
    > > //some private date
    > > public:
    > > BasicMidReader(char* FN)
    > > {// some code}
    > > ~BasicMidReader() {// some code}
    > > };

    >
    > I haven't used Borland C++, but just a guess:
    >
    > "virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base
    > class" sounds like TMemoryStream could have a virtual destructor, and you
    > haven't declared ~BasicMidReader() as virtual. If so, I would expect this

    to
    > be a warning rather than an error, since inherited virtual functions are
    > also virtual - even if they don't say so (but they should !)


    Why should they? If the language says that 'if any base class has
    a virtual destructor, the class' destructor is virtual even if not
    declared such', why declare it virtual? It's virtual already.

    > (BTW: in general, all destructors should be virtual unless you are sure

    the
    > class will never be used as a base class - this even applies if the
    > destructor is empty (ie. there is nothing to explicitly delete)).


    That is nonsense. Even if you use some class as a base class for
    something else, its destructor doesn't have to be virtual if you
    will never delete a dynamically allocated object polymorphically.

    Declaring functions virtual when there is no need, only leads to
    performance degradation and wasted space.

    Victor
    Victor Bazarov, Nov 29, 2003
    #3
  4. tomek

    David Fisher Guest

    "Victor Bazarov" <> wrote:
    >>
    >> inherited virtual functions are
    >> also virtual - even if they don't say so (but they should !)

    >
    > Why should they? If the language says that 'if any base class has
    > a virtual destructor, the class' destructor is virtual even if not
    > declared such', why declare it virtual? It's virtual already.


    Just to make it clearer to the reader.

    >> (BTW: in general, all destructors should be virtual unless you are sure

    the
    >> class will never be used as a base class - this even applies if the
    >> destructor is empty (ie. there is nothing to explicitly delete)).

    >
    > That is nonsense. Even if you use some class as a base class for
    > something else, its destructor doesn't have to be virtual if you
    > will never delete a dynamically allocated object polymorphically.
    >
    > Declaring functions virtual when there is no need, only leads to
    > performance degradation and wasted space.


    Of course; I should have said, "unless you are sure the class will never be
    used as a polymorphic base class." But assuming that the writer does not
    have complete control over the future use of the class (which is most of the
    time in a software company), you never know how the class is going to be
    modified over time. It is also easy to forget to make a destructor virtual,
    and hard to track this down if there is a problem (it happened to me !)

    David F
    David Fisher, Nov 30, 2003
    #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. tomek
    Replies:
    2
    Views:
    664
    Andrey Tarasevich
    Dec 1, 2003
  2. Chunhui Han
    Replies:
    2
    Views:
    490
  3. Andreas Lagemann
    Replies:
    8
    Views:
    457
    Mike Wahler
    Jan 10, 2005
  4. Replies:
    11
    Views:
    438
    Steven Bethard
    Nov 16, 2005
  5. Replies:
    7
    Views:
    368
    Richard
    Aug 28, 2006
Loading...

Share This Page