I’m wondering if something like this is possible…
class Thing
{
public Thing(int i)
{
}
}
class DerivedThing : Thing
{
public DerivedThing(int i)
{
}
}
_thing = new Thing(0)
_derivedthing = new Thing(1)
If you pass 0 you get a Thing, if you pass 1 you get a DerivedThing
This is not complete, just an illustration.. But basically I’m wondering if/how you could return different derived classes based on the value of a parameter passed to the baseclass constructor?
Or do you just need another bit of code which decides which constructor to call?
To answer your question: no, it is not possible. But…
You are actually looking for a
Factorypattern. You can easily add a distinguishing if/case in a factory method and still have a relatively clean code.Factory pattern description