Replies: 4 comments 21 replies
-
Hope to see it done |
Beta Was this translation helpful? Give feedback.
-
In addition to propagating constructors "as is", it would be very helpful to "extend" them, as well. I create a discussion for this: 5601. class Base
{
public Base(int x, int y) { ... }
}
class Child : Base
{
public Child(int z) : base() { ... }
// this actually becomes
public Child(int x, int y, int z) : base(x, y) { ... }
} I'm not sure what syntax to use here; I imagine this can be done with some new keyword, or using an existing contextual keyword, or some symbol. Basically, if the base class only has one "visible" (not private) constructor, we allow a child class to "extend" this constructor - and thus saving a lot of typing and maintenance. |
Beta Was this translation helpful? Give feedback.
-
You can use today source generators: https://www.nuget.org/packages/Constructor.Inheritor/ |
Beta Was this translation helpful? Give feedback.
-
It's 2025 and we still have to manually write out empty child class constructors that just call base? This really should be a part of the standard. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@TonyValenti commented on Thu Jan 28 2016
Right now if a base class has a constructor with parameters, all derived classes have to basically reimplement the constructor even if there is no functional change. If a class does not specify ANY constructors, I want it to automatically "inherit" all of it's bases constructors. This kind of already happens today with the default constructor but I would like this extended to all constructors.
Here are a few code examples that show what I want to do.
In short, I want DerivedV2 to automatically "inherit" all of Base's constructors so that I don't have to litter my code with constructors that just pass values down the chain.
@HaloFour commented on Thu Jan 28 2016
For reference, this is the syntax that was added in C++11:
@alrz commented on Thu Jan 28 2016
What if I don't want to inherit every constructor? There should be some notation to specify exactly those ctors that you want to inherit, either in the base class or the derived class.
@MgSam commented on Thu Jan 28 2016
@alrz There is- exactly what you have today.
@alrz commented on Thu Jan 28 2016
@MgSam Yep, so this is just a matter of tooling I guess.
@bondsbw commented on Thu Jan 28 2016
@alrz If you want to remove a constructor from the set of inherited constructors, simply fall back to the existing syntax where you specify them all manually. Including any new constructor signature erases all constructor inheritance.
Otherwise if you just want to alter one constructor from that set, you can include that constructor with the same signature and the rest would inherit. (Not sure if that was the proposal but I'm suggesting it as a possibility.)
@alrz commented on Thu Jan 28 2016
@WrongBit commented on Wed Jul 06 2016
If you derive class from parent, isn't it your intention to repeat behaviour of base + add your own? If you wanna hide base constructors, why not to introduce special keyword? Example:
Here we have ButtonWithImage, which should not have any text (that's why we should hide constructor). But by default constructor should be inherited and moreover - be callable by developer at any place of derived constructor (like it done in normal languages):
This way we have full control on what and where should be done. Theory shines only on paper, but in reality we need "dirty" way, because clean theory cannot cover real life.
@HaloFour commented on Wed Jul 06 2016
@WrongBit
Behavior, yes. Initialization, no. Not having constructors automatically inherited is very explicitly intentional on the part of most OOP languages as the nature of initialization is different for every class. It does not fit in the same category as
virtual
members.C++11 and Groovy both added explicit syntax to opt-into constructor inheritance.
And automatically propagate the constructors without that keyword? That would represent a breaking change to the language, which makes it effectively a non-starter.
@HaloFour commented on Wed Jul 06 2016
This seems like it would be simple enough to solve with source generators:
Beta Was this translation helpful? Give feedback.
All reactions