Results 1 to 7 of 7

Thread: Do Virtual Destructors exist in C++ ?

  1. #1
    Join Date
    Dec 2010
    Posts
    59

    Do Virtual Destructors exist in C++ ?

    I have a bit of idea about the Constructors , Virtual functions , destructors, and all those things , but I was a bit confused about the Virtual Constructors , does any thing like that exist, if it does Can any one please elaborate, I have heard that it has something to do with the Virtual functions and abstract classes , is that true , please give your valuable suggestions , I am waiting for the response .

  2. #2
    Join Date
    Apr 2009
    Posts
    569

    Re: Do Virtual Destructors exist in C++ ?

    Unlike normal member functions, a virtual destructor is not overridden when being defined again in the child class. Rather, it is extended: the lower -most destructor initial calls the destructor of its parent class and only then, it is run . as a result, when you attaemt to declare a pure virtual destructor, you may come across compilation errors, or even more bad: a runtime crash. However, there's no need to worry , you can have the benefit of both worlds by declaring a pure virtual destructor without a menace The abstract class should have a declaration (excluding the definition) of a pure virtual destructor:

  3. #3
    Join Date
    May 2009
    Posts
    637

    Re: Do Virtual Destructors exist in C++ ?

    When there is a pure virtual function in any class then it stops us to create any object of of that class. That is we cannot instantiate that class.Now if you wish to prevent a class from being instantiated but at that instant of time you do not wish to declare any pure virtual function within it, then there is no means apart from that you make your destructor as pure virtual. There is no any such means to make your constructor as pure virtual and you do not have any other function to make pure virtual then in that situation you are left with only one choice of making your real destructor as pure virtual to prevent your class to be instantiated.

  4. #4
    Join Date
    May 2009
    Posts
    543

    Re: Do Virtual Destructors exist in C++ ?

    Well if you really want your work to be done and take my opinion then I will say that I do not consider that is right. To stop creation of any object of a class you can merely declare constructors as either private or protected. This method is much more practical than declaring pure virtual destructor. I suppose, declaring pure virtual destructor may be used in interface classes, when you require to import class from a DLL .

  5. #5
    Join Date
    May 2009
    Posts
    529

    Re: Do Virtual Destructors exist in C++ ?

    While pure virtual destructors are official in C++, there is an additional check when using them: you have to provide a method body for the pure virtual destructor. This appear counterintuitive; how can possibly a virtual method be “pure” if it requires a function body? But if you bear in mind that constructors and destructors are particular operations it creates more logic, particularly if you consider that all destructors in a class ladder are always invoked.

  6. #6
    Join Date
    May 2009
    Posts
    511

    Re: Do Virtual Destructors exist in C++ ?

    Consequently, if parent class only has the one constructor that takes a handle, you will be strained to deploy a child class constructor that calls the parent class constructor with the accurate arguments (which you will be required to program into your child class constructor's initialization list). Whether the child class handle has the similar signature or not is unrelated. It is up to you to make a decision how to program that child class which can produce handles as fraction of its behavior or permit the invoker of the constructor to pass in the handle. Either method, the derived class will construct the parent class and could be required to render a handle, somehow just be restraining the types of constructor(s) declared and defined inside the parent class.

  7. #7
    Join Date
    Apr 2009
    Posts
    488

    Re: Do Virtual Destructors exist in C++ ?

    As far as ordinary practice, when you derive from a parent class, make both the destructors virtual. This is permits for runtime binding with the compiler. Its like a way of making it "smart" and able to differentiate from when its invoking a parent or child classes constructor/destructor. It is also a little sluggish, so if you are not deriving from a class, do not make use of it. First, as has already been deduced you cannot have virtual constructors. It is not achievable, so lets take that off the table straight away. The reason of virtual functions is to permit a caller to call a function from a parent class pointer. Constructors are very unlike from other member functions so the idea of a virtual constructor does not make any wisdom. Only non-static member methods (not including all forms of constructors) can be virtual. I hope this information was cooperative. I think your doubt would have cleared from these posts.

Similar Threads

  1. Constructors and Destructors in PHP
    By garfield1 in forum Software Development
    Replies: 4
    Last Post: 02-03-2010, 11:01 PM
  2. Does anyone know about virtual constructors and destructors?
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 01:57 PM
  3. Replies: 2
    Last Post: 02-06-2009, 10:53 PM
  4. What are the Constructors and Destructors?
    By RupaliP in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 07:03 PM
  5. migrate Hyper-V virtual machine to virtual server/virtual PC 2007
    By Larry Scott in forum Windows Server Help
    Replies: 1
    Last Post: 18-03-2008, 03:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,873,800.73712 seconds with 16 queries