Shah provides a surgical breakdown of D’s inheritance model, highlighting how the language elegantly resolves the legacy frustrations of C++ through sensible defaults. It is a masterclass in technical conciseness for those who value architectural precision over boilerplate.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Classes - delegating constructors, super and virtual destructor-part 5 of N [Dlang Episode 143]Added:
Hey, what's going on folks? It's Mike here and welcome back to my D programming language series. In today's episode we're going to be talking a little bit about constructors in the D programming language as they relate to classes. And in particular we're going to be talking about super and just a few of the other things that you should know about classes. So, with that said, let's go ahead and dive in. We're going to go ahead to the documentation and the language reference here. And let's go down to classes and let's go ahead and search for constructors so we can learn a little bit about how these work here.
And we'll also touch a little bit on destructors as well. I think that'll be useful uh for us as well, but uh let's go ahead and search around here. Here we are to constructor. So, again, I think we've already talked a little bit about this and we've seen these with structs here. And uh the first thing that I want to note for you is that by default D will give you a constructor or generate one um for you at least in your base class implicitly um if you do not define one. Okay? And this is going to be kind of similar to in like C++ or other languages where the compiler will just generate uh something for you.
Now, of course, what goes into the body here um well, we'll have to discover here. So, let's just go ahead and play around with this a little bit.
Um okay, let's go ahead and create a class. And if you watched the last video, we were working with monsters with power and health. And maybe we can give these like default values here something different here.
And then my question is if I just go ahead and uh let's just go ahead and create a monster equals new monster and let's write out the monster's power uh and the monster's health here. Okay?
health And let's go ahead and run this.
Uh let's see here. I forgot one semicolon. Some of you folks probably notice this.
Um and voila, there we go. Okay? So, we get the uh im- sort of implicit constructor, the compiler supplied constructor here. And of course, we've got these default initialized values here in our class. So, voila. Okay, so that seems to work for us. So, um you know, generally speaking I think it's a good idea to define constructors, um but we can kind of play around with these a little bit here.
Um okay, so let's see here.
Let's go ahead and play around with this a little bit. So, I want to go ahead and start with maybe this idea of uh delegating constructors because often times you'll have like set up code that you want to do here. So, what do I mean by uh delegating constructor? Uh maybe I'll have this here.
And maybe I'll go ahead and say power equals uh health * 2.
Uh I don't know, just like whatever work we want to do here.
And let's just go ahead and call this the delegating constructor.
Constructor constructor. There we go.
>> [laughter] >> And uh basically the idea is just to say that I can like reuse this constructor within another constructor call. So, let's say monster name Uh let's go ahead and do something like this.
And I can call this within here. Okay?
And I can say uh finishing the rest of, you know, monster name uh construction. Okay? Something like this.
And let's go ahead and give this a run here.
Um so, first and foremost, this code should still work because I have this constructor that I can run here, right?
And it's just going to call that delegating constructor.
Um now, what if I don't want that to run though? I mean, can I do like the Can I make this private? Let's see if that'll work here.
>> [snorts] >> Uh okay, we're going to have to move this to another file here, right?
Public's the default in the module, uh and we talked about that last time.
So, let's go ahead and split this into monster.d here.
And then I'll need to import monster here.
Okay? So, let's try to run this here.
Uh and there we go. Constructor, you know, is not accessible from module main, so this could be one strategy if you're trying to use like this common constructor in all of your other constructors if you have a bunch of those overloads. Um so, you know, the option's sort of yours there. That's maybe where I would use this feature here, right? This is like I'm just going to call this common construction code. Sometimes folks just write this out as like an init function or something like that, okay?
Uh initial, which is short for initialize, or something like that here, right?
They'll create a void initialize function that can be called within all of your constructors.
Um but, you know, it's just doing this within a constructor is fine. You could make it private if you want. Maybe some folks like this, maybe not.
Um the the reason that maybe you would put one of these accessors is to avoid situations like this, um where you want to be super explicit about, "Hey, every monster should have like a chosen name."
Uh so, uh I don't know.
Theodore, Theo, something like that here.
>> [laughter] >> All right, sounds like a pretty friendly monster.
Um okay.
And let's go ahead and give ourselves the uh standard library there.
Okay, so now this is uh working here, okay?
Uh or or rather this is a demonstration of, "Hey, we called this, you know, constructor, the public one, that's available.
And from within this constructor, right, we instantly called this delegating constructor, do the work here, power equals power times two, and then we finish the rest of our constructor our object, and then we printed things out here." And we could see that the uh power is now 10 uh in this uh particular instance here, okay?
All right, so hopefully that makes sense. That's one of the features that we have here. So, delegating uh constructors, that is totally totally allowed here.
Uh let's see here. Um now with that, uh, a little bit of a warning here, you can't create this sort of infinite loop here. Um, I don't know, it says the compiler is not required to detect this cyclic instruction. You'll probably find out when your code's hanging.
>> [laughter] >> That this is not allowed here. So, just be a little bit careful with that feature, but again, probably not a problem or again, you can get away with it by just using that initialize.
Uh, as I touched on earlier, um, you get the sort of implicit base class construction meaning that the compiler is going to, uh, generate a uh, basically an empty function like this and then it'll default initialize your member variables.
Um, and that is the basic idea here.
Okay, uh, let's see here. What else can we talk about here? Uh, there's all sorts of other interesting things here.
I didn't encourage you to read this.
There's basically one other thing that I want to talk about in today's video, but I'll just quickly show that you can, you know, add attributes, um, all these things to your constructors, pure, and so on. Um, uh, so kind of neat, um, uh, things that you can play around with in the D programming language.
Um, let's see here. Ah, yes. Okay. Um, so actually there's two things that I want to talk about. And so, first we're talking about constructors, um, I do want to talk about destructors just briefly here. That's these guys. I mean, we've looked at these briefly, these special member functions. Again, we've seen how these are called. We looked at in a video, maybe two videos ago, about how destroy is used for instance. Um, the one thing that I do want to just mention here, this will be nice for the C++ folks here. Virtual, uh, destructor.
Uh, let me see if I can find it in here.
Uh, let's see here.
Uh, yes, here is the note. Okay, for my C++ folks, I know this comes up. When you have inheritance hierarchies, right, in C++, you got to make sure that your destructors are virtual. In D, you don't have to worry about that silly rule.
Destructors are always virtual. Okay, there's no special keyword. You just write your destructor. One less thing in the world to worry about.
>> [laughter] >> So, that's kind of nice here. Okay, no parameters and so on. So, let me go ahead and just define our destructor.
Uh and I'll make that note. Destructors are always virtual.
And let's go ahead and say destroying monster.
Okay, so there we are.
Um there's uh monster. So, let's go ahead and run this. And again, eventually some at some point in time, again, non-deterministically, just to review that. So, probably sometime after this main when our program's turn uh terminating, the garbage collector will go through and destroy all of our objects, which calls the destructor, and we eventually destroy our monster here.
Okay? So, that's the idea here. And we learned about some other tricks about again in a previous video of making this scope, um which gives us opportunity to be stack allocated and destroyed somewhat um within a uh or in the case of scope, more deterministically, let's say, within that scope. Um that's the idea.
Okay.
Um now, what becomes a little bit interesting with constructors, and let me go ahead and shrink this down here, is when I have a derived class here.
And we were working with dragons last time, so let's bring those back.
And uh derive here.
And let's go ahead and say I have my constructor here creating a dragon.
Okay.
Um and now, let's go ahead and create a new dragon.
We're going to call just this dragon constructor here.
Uh let's see what happens here. Okay, so I'm going to create something that is a type of monster. It's a dragon.
So, let's ask ourselves which constructor is going to be called for the dragon. Well, it looks like we have one option.
Uh, which constructor is going to be called for uh, our monster.
Uh, cuz we're going to our monster we have this like delegating constructor.
Uh, but we also have like this here, right? Uh, where we said, "Hey, we need to make the name of the thing." So, so how is this dragon get constructed?
Okay, let's go ahead and take a look here.
I'm going to go ahead and run this with RDMD. Hopefully, you've placed your bets and the answer is well, uh, it calls delegating constructor first.
Why? Well, dragon is a type of monster.
So, you got to do all the monster stuff, all the creation of the monster stuff.
Now, interestingly, uh, and then we then we do the dragon constructor. So, it chose this one here.
But, you might have said, "Hey, Mike, um, you know, you were saying, 'Hey, we're going to use this as like this private thing that, you know, nobody was supposed to call.' And then we wanted to call these other constructors and, you know, this one's allowed to use the delegating constructor cuz that'll do all the common stuff for monsters that we wanted like put in some code somewhere."
Um, but how do we call then the constructor, uh, you know, for our our super class, the class that we're deriving from, from monster? Well, that is what the super keyword is for. Okay, so we do super and then we would put in the monster's name.
So, I'm going to put, uh, fill in the blank dragon name here.
And then that will actually call, uh, and let's make sure that we, uh, let's make this a little bit, uh, uh, larger here. Sorry, I'm just doing this one key at a time here. Uh, grow up you five plus. There we go. Okay.
>> [laughter] >> Uh now now ask yourself, so I'm constructing this dragon here.
It's going to call the super uh with or with super rather, it's you know, calling for the class that we're derived from cuz we've just got one level of inheritance here. So, you look up at our uh class that we're deriving from, which in this case is monster. This signature matches this super function here.
Okay, so we're going to execute this code. Uh and then from this code, right, we'll call our delegating constructor to set up whatever we need. And then finish the rest of our monster, and then eventually create our dragon. Okay, so let's go ahead and run this and see if that follows.
So, we go into super.
First thing that that does is call the delegating constructor. We do that work.
And then we do finishing the rest of our monster name here. So, that's step two.
And then once we finished that, we go into write line here. Okay? So, hopefully that makes sense.
And the nice thing is, I mean, you have explicit control if you haven't seen this. So, I could do this super, you know, later after creating the dragon if I wanted to print that out. Generally speaking, I do these things first, but on occasion you might have code you know, before you do this. For instance, maybe you pass in some parameters here and you want to like validate those. We haven't talked about contracts yet, but maybe you'd have like an assert statement or something to say, "Hey, um you know, whatever property here you want to check from some arguments here. Like if I put in the argument for power and I would say, you know, if power is less than zero, you know, do some error or something. So, okay, that would be a reason why you can kind of shift this around here. Okay, cuz you can avoid doing some work or or whatever. And then you can early return.
Okay?
Uh but anyways, um super is your way to from your derived or superclass um sort of choose which constructor is called. Okay? So, in this video, we've learned about two concepts here, or three rather, the delegating constructor, just for review here, which we could we could call as many of these as we want within our other constructors, but I would probably define one.
I would probably make it private, but this is something that we can argue about in the discussion section about why you may or may not do this, and why it's better to maybe just write an init function. That's fine. Just want to show you that you can do it. Um that's the purpose of this.
Um but then again, uh the other thing I showed you is destructors are always virtual.
So, somewhere you saw we were destroying the monster here. And again, just to show that off, why is that important?
Well, you know, eventually we need to destroy the dragon as well.
Cuz well, if I've gone through the effort of through my dragon constructing everything that it needs to be to be a monster, and then all the additional stuff to be dragon, I better, you know, kind of gone through both of those destructors here.
Uh and we'll see that's true in the reverse order. We destroy the dragon, and then destroy the monster. Okay?
Always taking care of. So, my C++ folks are clapping their hands uh and cheering >> [laughter] >> that they don't have to worry about, you know, forgetting [snorts] that keyword or whatever. Uh but that that's the idea here. Okay? And then again, the final thing we learned about is super, which will tell us which constructor and at exactly what point in time to call the constructor for the class uh from which you're deriving. In this case, monster's constructor. All right.
So, hopefully that made sense. Hopefully that was enjoyable, and you learned a thing or two here. Uh that's getting us through a lot of the object-oriented stuff that we can do in the D programming language. Of course, there's more, so we'll take a look at it, but otherwise, as always, feel free check out courses.msh.io. You know the deal.
Here's the D language lessons. There's a bunch of other fun stuff on there for you. So, thank you as always for your time and attention, folks. I'll look forward to seeing you in the next one.
And yeah, do let me know, are you using these features? Delegating constructors?
Do you have strong opinions about them?
Do you make some of your constructors private?
I mean, classically, making your constructors private is a way to do like a singleton pattern or these types of things. But anyways, I'd love to hear your thoughts.
If you have rationales or strong opinions about certain contexts in which you do stuff, so we can learn in the community.
Anyways, folks, I'll leave you with that and look forward to seeing you in the next episode.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











