Abstract virtual constructors #8545
Unanswered
CypherPotato
asked this question in
Language Ideas
Replies: 2 comments 5 replies
-
IMO, I'd rather it be a feature of the deriving class, that instructs the compiler to copy the constructors of the base class automatically, as it already does for parameterless constructors. That's how it works in C++: class A
{
public:
explicit A(int x) {}
};
class B: public A
{
using A::A; // this syntax in C++ causes the A constructors to be copied to B
}; |
Beta Was this translation helpful? Give feedback.
3 replies
-
I think this is a textbook scenario for a source generator. If a constructor with the given signature is defined on the derived class, the SG omits that one. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, if an abstract class has many constructors, those constructors must be specified in all the derived classes, which leads to a lot of code repetition. Note this example:
The example above contains three constructors. For each derived type, I need to implement all three constructors.
Note that I want to use the same implementation of the abstract constructor without changing its body. Therefore, I keep the constructor of Dog empty. My proposal is for virtual constructors, which do not need to be implemented in the derived classes to function, and yet still have an implementation.
In this way, every class that inherits from
Animal
will already have its constructor defined, and I will be able to call it as follows:I could also overload constructors if I wanted to.
Beta Was this translation helpful? Give feedback.
All reactions