in reply to Can a class constructor create an instance of a derived class and return it? Or, can objects transmute into other objects?

You might have heard of the Single Responsibility Principle which says that a class should only have one job to do (or only one reason to change).

Your SMNPDevice is holding down two jobs i.e. being a base class and being a factory (a factory decides what type of objects to create). A cleaner design would be to just let SMNPDevice be a base class, and introduce a separate factory (essentially what Corion's example does).

  • Comment on Re: Can a class constructor create an instance of a derived class and return it? Or, can objects transmute into other objects?