If a class can call its base constructor then why the constructor is not inherited
-
I miss the logic of the official story that a base constructor is not inherited. If a class is able to call its base constructor (accessible) then why it is said the constructor is not inherited. The logic seems to be based on the fact that a base constructor has to be called explicitly, meaning the compiler will not call it automatically (ignore special cases). But the definition of inheritance is not based on the automatic calling.
-
I miss the logic of the official story that a base constructor is not inherited. If a class is able to call its base constructor (accessible) then why it is said the constructor is not inherited. The logic seems to be based on the fact that a base constructor has to be called explicitly, meaning the compiler will not call it automatically (ignore special cases). But the definition of inheritance is not based on the automatic calling.
In C# (at least) a constructor is implemented as a static method -- and static methods are not inherited. Drives me nuts. See also: http://www.codeproject.com/Lounge.aspx?fid=1159&fr=51#xx0xx[^]
-
In C# (at least) a constructor is implemented as a static method -- and static methods are not inherited. Drives me nuts. See also: http://www.codeproject.com/Lounge.aspx?fid=1159&fr=51#xx0xx[^]
The question assumes an instance constructor. A static constructor is not meant to be called by any outside code or any other class (derived or not derived), it is called only once by the loader, so the issue of derivation is totally moot for static constructor.
-
The question assumes an instance constructor. A static constructor is not meant to be called by any outside code or any other class (derived or not derived), it is called only once by the loader, so the issue of derivation is totally moot for static constructor.
And my answer applies to instance constructors -- in C#.
-
I miss the logic of the official story that a base constructor is not inherited. If a class is able to call its base constructor (accessible) then why it is said the constructor is not inherited. The logic seems to be based on the fact that a base constructor has to be called explicitly, meaning the compiler will not call it automatically (ignore special cases). But the definition of inheritance is not based on the automatic calling.
CRobert456 wrote:
The logic is based on the fact the a base constructor has to be called explicitly, meaning the compiler will not call it automatically
That would mean that all objects (!) would inherit the default constructor. That'd be a bit impractical. You can add code to call an inherited constructor in your class, even if there's a different signature.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
In C# (at least) a constructor is implemented as a static method -- and static methods are not inherited. Drives me nuts. See also: http://www.codeproject.com/Lounge.aspx?fid=1159&fr=51#xx0xx[^]
That just sounds wrong. From the C# 5 lang spec: section 1.6.6.3
The ... constructor (which is like an instance method) ...
Because the constructor is an instance member, it is permitted to access both the ... instance field and the ... static field.The constructor has access to all of the instance's fields and properties and even instance methods. Section 1.6.7.1
Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.
Section 10.11.1:
If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitly provided. Thus, an instance constructor declaration of the form
C(...) {...}
is exactly equivalent to
C(...): base() {...}A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.
-
In C# (at least) a constructor is implemented as a static method -- and static methods are not inherited. Drives me nuts. See also: http://www.codeproject.com/Lounge.aspx?fid=1159&fr=51#xx0xx[^]
As I recall, the default base ctor is always called first, at least it is if the base class contains instance properties that need initialization.