<p>How to make sure that all derived C++/CLI classes will override the ICloneable::Clone() method of the base class?</p>

<p>Do you think I should worry about this? Or this is not a responsibility of the base class' writer?</p>

<p><strong>Amendment:</strong> Sorry, I forgot to mention that the base class is a non-abstract class.</p>

[{'answer_id': 79262, 'author': 'Thomas', 'author_id': 14637, 'author_profile': 'https://Stackoverflow.com/users/14637', 'pm_score': 1, 'selected': False, 'text': '<p>Declare the Clone() method as abstract. This should work even when the parent class does have a concrete implementation.</p>\n\n<p>Of course, the risk when enforcing such things is that the writer of the derived class will become annoyed, say "I\'m not going to use Clone anyway" and does something like a bytewise copy, or even a "return this", to get rid of the errors.</p>\n'}, {'answer_id': 79324, 'author': 'Milan Babuškov', 'author_id': 14690, 'author_profile': 'https://Stackoverflow.com/users/14690', 'pm_score': 2, 'selected': False, 'text': '<p>Declare it pure virtual in the base class.<br></p>\n\n<p>class Base<br> \n{<br>\n...<br>\nvitual void Clone() = 0;<br>\n};</p>\n'}, {'answer_id': 79327, 'author': 'Philip Rieck', 'author_id': 12643, 'author_profile': 'https://Stackoverflow.com/users/12643', 'pm_score': 2, 'selected': False, 'text': '<p>Well, I can\'t say if this is the responsibility of the base class or not, and won\'t get into the perils of inheritance based contracts here.</p>\n\n<p>In any case, you <em>can</em> force some class to override a method - "Clone()" for example, by making it a <em>pure virtual</em> member of an abstract class</p>\n\n<pre><code>public ref class ClonableBase abstract\n{\n public:\n   virtual void Clone() = 0;\n}\n</code></pre>\n\n<p>note the "abstract" and the "=0;".  The abstract allows the class to contain pure virtual members without warning, and the =0; means that this method is pure virtual - that is, it doesn\'t contain a body.   Note that you can not instantiate an abstract class.</p>\n\n<p>Now you can</p>\n\n<pre><code>public ref class ClonableChild : public ClonableBase\n{\n public:\n   virtual void Clone();\n}\n\nvoid ConableChild::Clone()\n{\n  //some stuff here\n}\n</code></pre>\n\n<p>If you do NOT have the Clone override in ClonableChild, you get a compiler error.</p>\n'}, {'answer_id': 79331, 'author': 'Jon', 'author_id': 12261, 'author_profile': 'https://Stackoverflow.com/users/12261', 'pm_score': -1, 'selected': False, 'text': "<p>Thomas is correct but one way you would make that class abstract is to define a pure virtual method.</p>\n\n<p>This is done by saying:</p>\n\n<p>virtual void Clone() = 0;</p>\n\n<p>Unless the derived class implements Clone they won't be able to instantiate it so they'll have little choice if they want their class to be useful.</p>\n"}, {'answer_id': 79437, 'author': 'metao', 'author_id': 11484, 'author_profile': 'https://Stackoverflow.com/users/11484', 'pm_score': 1, 'selected': False, 'text': '<pre><code>class Base\n{\n...\nvirtual void Clone() = 0;\n};\n</code></pre>\n\n<p>is correct.</p>\n\n<p>If you want some default behaviour for Clone, try:</p>\n\n<pre><code>class Base\n{\n...\nvirtual void Clone()\n{ \n   ...\n   doClone();\n   ...\n};\n\n...\n\nprivate:\nvirtual void doClone() = 0;\n};\n</code></pre>\n'}, {'answer_id': 79945, 'author': 'Thomas', 'author_id': 14637, 'author_profile': 'https://Stackoverflow.com/users/14637', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p><strong>Amendment:</strong> Sorry, I forgot to mention that the base class is a non-abstract class.</p>\n</blockquote>\n\n<p>In this new light, I'm pretty sure that you do not want to force anyone to override Clone() at all. For example, if my derived class does not add any fields it probably does not need its own specialized Clone() method.</p>\n"}, {'answer_id': 80068, 'author': 'Eric W', 'author_id': 14972, 'author_profile': 'https://Stackoverflow.com/users/14972', 'pm_score': 1, 'selected': False, 'text': '<p>If the base class is non-abstract, then there is no way to force it to be overridden at compile time.  The best you can probably do is something like:</p>\n\n<pre><code>virtual void Clone()\n{\n   throw gcnew NotSupportedException();\n}\n</code></pre>\n\n<p>With this, derived classes would have to override the method or your application will encounter a NotSupportedException.  This at least would make it immediately obvious during testing that something was incorrect.  It would give you something to look for so that you know when you encounter a class that did not correctly override Clone.  Depending on how much control you have over derived classes, this could be important for robustness.</p>\n'}, {'answer_id': 80108, 'author': 'metamal', 'author_id': 14385, 'author_profile': 'https://Stackoverflow.com/users/14385', 'pm_score': 0, 'selected': False, 'text': '<p>After some pondering I found this solution:</p>\n\n<pre><code>Object^ BaseClass::Clone()\n{\n  if(this-&gt;GetType() != BaseClass::typeid)\n  {\n    throw gcnew System::NotImplementedException("The Clone() method is not implemented for " + this-&gt;GetType()-&gt;ToString() + "!");\n  }\n\n  BaseClass^ base = gcnew BaseClass();\n  ... // Copy the fields here\n  return base;\n}\n</code></pre>\n\n<p>It throws NotImplementedException if you attempt to Clone an instance of a derived class that hasn\'t overridden the Clone() method of the base class.</p>\n'}, {'answer_id': 82127, 'author': 'ugasoft', 'author_id': 10120, 'author_profile': 'https://Stackoverflow.com/users/10120', 'pm_score': 0, 'selected': False, 'text': '<p>read <a href="http://www.gotw.ca/gotw/071.htm" rel="nofollow noreferrer">this</a> by herb sutter. It\'s exactly what you are asking</p>\n'}]
['https://Stackoverflow.com/questions/79244', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14385/']