c++ - Virtual Function During Construction Workaround -
I have found a base class that has a virtual function; I want to call that class as it is because I want to The function for each of the derived classes is called. I know that I can not call a virtual function during the creation, but I can not think of an elegant solution (i.e., avoid repetitive code) solution.
What are some things around to do a virtual function call during construction? The reason for this is that I want to avoid it because I do not want to make consultants who just call the base class. Python way to do this: This is the factory method approach, putting the factory in the base class: Added a Usage: Class A {Public: A () {read (); } // read it at anytime Virtual Zero () = 0; } Class B: A {Public: B (): A () {}; Read () {/ * Do something special for B *}} Category C: A {Public: C (): A () {}; To read () {/ * something special to C PSY:
raiseImplementedError raise in
one :: read () . I'm returning to C ++ and I thought it was worse than that.
clone -method with covariant return type as a bonus.
Class A {Public: // It should never be read from Virtual Z () = 0; Virtual A * Clone () const = 0; }; Template & lt; Class D, Class B & gt; Struct CRTP: B {D * Clone () {New D (* this);} Fixed D * Create () {New D (;)}} Return; Class B: Public CRTP & lt; B, A & gt; {B () {}; Public: Read Zero () {/ * Do something special for B *}}; Class C: Public CRTP & lt; C, A & gt; { C() { }; Public: Read Zero () {/ * something special for C *}};
Comments
Post a Comment