Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Lets talk about Virtual Destructors...

Lets talk about Virtual Destructors...

Scheduled Pinned Locked Moved The Lounge
question
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ammar
    wrote on last edited by
    #1

    What is a virtual destructor? What are the advantages and disadvantages of using it?

    T M 2 Replies Last reply
    0
    • A Ammar

      What is a virtual destructor? What are the advantages and disadvantages of using it?

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      A vitrual destructor is just the destructor of the class made virtual. (i.e. virtual ~MyClass ()). What it allows you to do is have methods in a base class handle the deletion of the object and still have the proper set of destructors be invoked. A perfect example from COM is having the base class define the Release method where it deletes the object when the ref count goes to zero. Tim Smith Descartes Systems Sciences, Inc.

      P 1 Reply Last reply
      0
      • T Tim Smith

        A vitrual destructor is just the destructor of the class made virtual. (i.e. virtual ~MyClass ()). What it allows you to do is have methods in a base class handle the deletion of the object and still have the proper set of destructors be invoked. A perfect example from COM is having the base class define the Release method where it deletes the object when the ref count goes to zero. Tim Smith Descartes Systems Sciences, Inc.

        P Offline
        P Offline
        Paul Westcott
        wrote on last edited by
        #3

        and... The disadvantage of having a virtual destructor is that if you have one, but have no virtual functions, then you are still being hit with a virtual table pointer. So you really shouldn't include a virtual destructor unless you are going to derive from the class. (and usually a good indication of that is if you have any virtual functions!) Paul. Have fun, Paul Westcott.

        A 1 Reply Last reply
        0
        • A Ammar

          What is a virtual destructor? What are the advantages and disadvantages of using it?

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          This seems to be a popular interview question. I've been asked this in about 1/3 of my job interviews over the past ~7 months. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!

          L 2 Replies Last reply
          0
          • M Michael Dunn

            This seems to be a popular interview question. I've been asked this in about 1/3 of my job interviews over the past ~7 months. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            >> I've been asked this in about 1/3 of my job interviews over the past ~7 months So you change your job at every ~2 months. What's wrong (or right) with you, mate?

            1 Reply Last reply
            0
            • P Paul Westcott

              and... The disadvantage of having a virtual destructor is that if you have one, but have no virtual functions, then you are still being hit with a virtual table pointer. So you really shouldn't include a virtual destructor unless you are going to derive from the class. (and usually a good indication of that is if you have any virtual functions!) Paul. Have fun, Paul Westcott.

              A Offline
              A Offline
              Ammar
              wrote on last edited by
              #6

              What do you mean by "being hit with a virtual table pointer" ? How come this is an advantage? Ammar

              P 1 Reply Last reply
              0
              • M Michael Dunn

                This seems to be a popular interview question. I've been asked this in about 1/3 of my job interviews over the past ~7 months. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I interview around alot too, and you are right,that is suddenly a very popular question. Even in Indiana. Weird.:confused:

                1 Reply Last reply
                0
                • A Ammar

                  What do you mean by "being hit with a virtual table pointer" ? How come this is an advantage? Ammar

                  P Offline
                  P Offline
                  Paul Westcott
                  wrote on last edited by
                  #8

                  not advantage - i said disadvantage (you have an extra 4 bytes per object) Paul. Have fun, Paul Westcott.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups