Go Back   TechArena Community > Software > Operating Systems
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-01-2011
Member
 
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 .
Reply With Quote
  #2  
Old 18-01-2011
Member
 
Join Date: Apr 2009
Posts: 567
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:
Reply With Quote
  #3  
Old 18-01-2011
Member
 
Join Date: May 2009
Posts: 620
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.
Reply With Quote
  #4  
Old 18-01-2011
Member
 
Join Date: May 2009
Posts: 533
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 .
Reply With Quote
  #5  
Old 18-01-2011
Member
 
Join Date: May 2009
Posts: 525
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.
Reply With Quote
  #6  
Old 20-01-2011
Member
 
Join Date: May 2009
Posts: 503
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.
Reply With Quote
  #7  
Old 20-01-2011
Member
 
Join Date: Apr 2009
Posts: 484
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.
Reply With Quote
Reply

  TechArena Community > Software > Operating Systems
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Do Virtual Destructors exist in C++ ?"
Thread Thread Starter Forum Replies Last Post
Constructors and Destructors in PHP garfield1 Software Development 4 02-03-2010 11:01 PM
Does anyone know about virtual constructors and destructors? Ram Bharose Software Development 5 08-02-2010 01:57 PM
Error on copying Virtual hard disk of Virtual PC to new computer Azariah Windows Software 2 02-06-2009 10:53 PM
What are the Constructors and Destructors? RupaliP Software Development 4 27-02-2009 07:03 PM
migrate Hyper-V virtual machine to virtual server/virtual PC 2007 Larry Scott Windows Server Help 1 18-03-2008 03:32 AM


All times are GMT +5.5. The time now is 10:18 PM.