c++ - How to force all derived classes to implement a virtual method? -


Say that you have a base class Dep for a tree of orbits is a virtual method < Code> Dep * Dep :: create () is that I want to implement it by every single leaf class. Is there any way to implement it?

Note: The problem here is that to apply this method, make intermediate classes (such as class B: public A: public dep code> A :: Code>) or because they think are those leaf classes, but in fact they are sub-classified.

The question ends here.

Reference

If you are curious why do I need it; I have a class master in which the unknown concrete type of Dep objects are repeated if I master is repeated, then I Dep example mail clone. Even after this, I can not catch it (secondly by crashing so badly), because for unclear reasons, people who have more to say than me, in this project dynamic_cast has been declared unlawful (perhaps it There is a good decision, but still totally different discussions).

Curiously using recurring template fun, you can get something similar:

  template & lt; Typename T & gt; Class Clonable: Public T, Public Department {Private: Clonable & lt; T & gt; (): T () {} Public: stable clonal & lt; T & gt; * Create () {New Clone Eligible & lt; T & gt; (); } Cloning & lt; T & gt; * Clone () {New Clonable Back & lt; T & gt; (* this); }}; Instead of using   

deep and using new MyType instead of clonable . Since is clonable & lt; MyType & gt; From to MyType , you can use the same way you would use the MyType , except that it is currently in the Depre :: Clone .

In addition, your master should not accept an example of type Dep , but apply it to a clonal & lt; T & gt; . (Change your function with a simple function template, which implements it.) It guarantees that any Dep inside the master is correctly implemented clone Function.

Since can be cloned & lt; MyType & gt; does not have any public constructor, it can not be inherited, although your actual MyType is more in heritage and can be used already.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -