Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In .NET can a class have virtual constructor?

Tags:

Can a class have virtual constructor??

If yes, why it is required?

like image 991
Ram Avatar asked Sep 06 '10 12:09

Ram


People also ask

Can a class have virtual constructor?

In C++, the constructor cannot be virtual, because when a constructor of a class is executed there is no virtual table in the memory, means no virtual pointer defined yet. So, the constructor should always be non-virtual. But virtual destructor is possible.

Can we define virtual constructor?

[20.8] What is a "virtual constructor"? An idiom that allows you to do something that C++ doesn't directly support. You can get the effect of a virtual constructor by a virtual clone() member function (for copy constructing), or a virtual create() member function (for the default constructor).

Do we need virtual constructor functionality if yes then why it is required?

1) Virtual constructor DO NOT exists. Because defining something as virtual means ,you are actually assigning a pointer the virtual look up table for that object and not actually creating that object. So virtual constructor is not at all possible. 2) We do not need virtual constructor functionality.It do not exist.

Can abstract class have constructors in C#?

Yes, an abstract class can have a constructor, even though an abstract class cannot be instantiated.

Does a class need a constructor C#?

If you do not provide any constructors for your class or struct, C# is going to create one by default. This, in turn, instantiates the object and sets member variables to the default values based on their datatypes.


2 Answers

no, a class cannot have a virtual constructor.

It doesn't make sense to have a virtual constructor. The order in which objects are constructed in C# is by constructing derived classes first, so the derived constructor is always called since the class you want to call is well known at the time of construction.

The other thing is, if you actually type this code out, you can quickly see that it makes very little sense at all

If you had:

public class BaseClass  {     public virtual BaseClass()     {     } } 

and then

public class InheritedClass : BaseClass {     //overrides baseclass constructor but why would you do this given that the          //constructor is always going to be called anyway??     public override InheritedClass()     {     } } 
like image 150
lomaxx Avatar answered Sep 20 '22 13:09

lomaxx


A virtual method is by definition a method which is dispatched based on runtime type analysis of the receiver, not the compile-time static type analysis of the receiver.

A constructor is a method called when a new instance of a particular type is created.

Since the runtime type of a newly created object is always the same (*) as its compile-time type, there is no need for virtual constructors: the runtime dispatch would always choose the same method as the static dispatch, so why bother making a difference?

(*) This is not entirely true; there are scenarios involving COM interop in which the runtime type of a construction is not the compile-time type. Many things are weird in the world of legacy interop code.

like image 44
Eric Lippert Avatar answered Sep 20 '22 13:09

Eric Lippert