Why destructor cannot be virtual
So we have a Print routine in our Player class. The concrete classes will specialize this and write code to Print their own data. Now suppose we have a class Squad. This class contains a list of Players in the Squad. The list will hold pointer of Player objects. At runtime we insert the objects in the list as per the type of game. We need to Print the Players in the squad. So we go through the list and call the Print routine of every Player in the list. This is fine, but we need to Print all the information of the Player which is game specific.
Hence we make the Print routine in Player as virtual. Now when Print is called on Player the proper Print routine will get called as per the type of Player. The virtual function mechanism enables to call the proper routine as per the type of object the pointer points to. Need of virtual destructors — At the end of the program Squad needs to be destroyed. While destroying the Squad, as good programmers, we will cleanup the contents list.
We will delete the Player in the list. But once again we will face the same problem we had faced with Print. Once again virtual mechanism comes to our rescue. If we make the destructor virtual, the proper destructor will get called as per the type of Player. The same mechanism we have for functions. Why no need of virtual constructors? If we see the conditions in which virtual was needed, every time we had a pointer to base class and wanted to call the functionality of derived class.
Hence no virtual is needed. Tony Delroy Tony Delroy The difference is that these clone and create functions don't work with containers, don't work with pass by value, etcetera. So they don't achieve what we want -- polymorphism without slicing while preserving ordinary value semantics. DavidSchwartz: clone and create don't work directly with Standard containers, but it's trivial to write a small management type that clone s from the copy constructor etc e.
Such management objects can also be passed around by value if you find that easier than using references. It's hardly trivial. The link goes to code that's already pretty complicated, and it's not enough to even make the standard containers work. Also, since it's not part of the language, it will be very difficult to make code that uses such a thing interoperate with code that doesn't.
From this link: docwiki. Marius Marius 3, 1 1 gold badge 27 27 silver badges 34 34 bronze badges. They exist since the loading of the code and static data of the executable. Correct, they are defined static and constant, but just not allocated and set up. They are setup at program startup. Rich No. Virtual function work in constructors exactly as anywhere else.
Virtual function calls are always based on the dynamic type of the object. Rich No: inside the base class ctor, a virtual call in a constructor will dynamically call the base class version, based on the dynamic type at the time: the base class. The virtual calls on the object under construction work the same whether they are in the ctor body or in any other function call by the ctor. The dynamic type of a base class subobject changes as construction of the derived class starts.
Show 4 more comments. So, if we try to declare virtual constructor compiler throw an Error: Constructors cannot be declared virtual. UpAndAdam 4, 2 2 gold badges 25 25 silver badges 41 41 bronze badges.
Neha kumari Neha kumari 71 1 1 silver badge 2 2 bronze badges. Now a logical answer to this question according to me is: The major use of virtual keyword is to enable polymorphic behaviour when we don't know what type of the object the base class pointer will point to.
Kushan Mehta Kushan Mehta 1 1 silver badge 11 11 bronze badges. Shraddha Sinha Shraddha Sinha 61 1 1 silver badge 1 1 bronze badge. Quick search will yield the answer: stackoverflow. Edouard A. Navneet Agarwal Navneet Agarwal 41 1 1 bronze badge. Tunvir Rahman Tusher Tunvir Rahman Tusher 5, 2 2 gold badges 33 33 silver badges 30 30 bronze badges.
For virtual destructor try this link. Rich Rich 3, 3 3 gold badges 22 22 silver badges 31 31 bronze badges. If we use a base class pointer to point to the base class object and call a virtual function using it. Then it would be late binding. But, will that be a run time polymorphism? Constructors are not static, period. This is obvious nonsense. A f g ; invokes a copy constructor. And there's an object it could be a member function of, g. Or void foo A a ; Here, we need to construct a new A to call foo , and look there's an object it could be a member function of -- f.
Constructors are not static, to get a static constructor we have to initialize static data members independently. Constructor don't have a name. They have a specific syntax which uses the name of the class. If that is the same case, how destructors can be virtual? We are not redefining the base destructor in derived class. But, this is not possible with a virtually implemented constructor. Thus, a virtual constructor itself would not have anywhere to look up to.
Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in C Language. We use cookies to ensure you have the best browsing experience on our website. For a base class without polymorphic deletion.
Either clients should not be able to delete polymorphically using a pointer to Base. BTW, In which case should use virtual destructors? For a base class with polymorphic deletion. If your destructor can never be invoked via a base pointer, making it virtual is unnecessary and redundant, especially if your class wasn't virtual before so it becomes newly bloated with RTTI.
When do I NOT need to use virtual destructors? Destructors shouldn't used in the first place in hard real time systems since many resources like dynamic memory cannot be used in order to provide strong deadline guarantees — Marco A.
Since when do destructors imply dynamic memory allocation? They're simply not used in my experience. It's also nonsense that dynamic memory cannot be used in hard real-time systems. It's fairly trivial to prove that a preconfigured heap with fixed allocation sizes and an allocation bitmap will either allocate memory or return an out-of-memory condition in the time it takes to scan that bitmap. Allowing compile-time checks of realtime guarantees. Show 2 more comments.
This is a good example of when to not use the virtual destructor: From Scott Meyers: If a class does not contain any virtual functions, that is often an indication that it is not meant to be used as a base class. Does anyone else remember the Good Old Days, where we were allowed to use classes and inheritance to build up successive layers of reusable members and behaviour, without having to care about virtual methods at all?
C'mon, Scott. I get the core point, but that "often" is really reaching. There are a few scenarios where a polymorphic base class is used in such a way that the destructor does not need to be virtual: If instances of derived classes are not allocated on the heap, e.
Except if you use uninitialized memory and placement operator new. If instances of derived classes are allocated on the heap, but deletion happens only through pointers to the most derived class, e. This is because shared pointers have their own dynamic dispatch for destructors the deleter that does not necessarily rely on a virtual base class destructor. Arne Vogel Arne Vogel 31 1 1 bronze badge. Constantinius Constantinius 3 3 bronze badges. MichelBillaud actually you can still have polymorphism without virtual dtors.
A virtual dtor is ONLY required for polymorphic delete, i. In short, there is a small overhead, but for small objects, it may make a difference. Mats Petersson Mats Petersson 2 2 silver badges 7 7 bronze badges. In addition This is only needed if anyone is allowed to delete a derived class through pointer to base. In many situations, that is not so. If you know it is, then sure, incur the overhead. Which, btw, is always added, even if the actual calls can be statically resolved by the compiler.
The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta.
0コメント