Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How are you going to know what type of instance to create? If you'll have an existing instance of the derived class and want to create another instance which is basically identical to it, you should have your base type implement a protected virtual <code>CloneBase</code> method which calls <code>MemberwiseClone</code> and does any deep copying the base type knows about, and a public <code>Clone</code> method which chains to <code>CloneBase</code> and casts to the base type. Each derived types should override <code>CloneBase</code> to chain to <code>base.CloneBase</code> and add any necessary additional deep copying (that step may be omitted if no additional deep-copy logic is required), and also shadow the public <code>Clone</code> method with one that chains to <code>CloneBase</code> and casts the result to its type [using a separate <code>CloneBase</code> makes it possible to both declare a new <code>Clone</code> method with a different signature while also overriding and chaining to the base-class method].</p> <p>If you'll have an existing instance of the new class, but want its properties to be copied from some <em>other</em> instance, could have an abstract <code>ConstructInstanceLike(x)</code> method which each derived type would implement to either call one of its constructors, or clone itself and modify the clone to match the passed-in object. Neither approach is terribly elegant, but either can work.</p> <p>If you won't have an existing instance of the new class, you'll need some other means of getting something of the appropriate type. The nicest approach is probably to store a collection of <code>Func&lt;TParams, TResult&gt;</code> delegates, one for each derived type of interest, and then invoke one of those functions to generate an object of one of the associated derived type. It would also be possible to define an interface</p> <pre><code>IFactory&lt;TParam, TResult&gt; { TResult Create(TParams param); } </code></pre> <p>but in many cases a delegate will be more convenient to work with.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload