c++ - Concept Based Polymorphism -


I'm reading on concept based legacy in C ++. I have a code sample attached to everyone I basically Asking whether this is the correct implementation of this concept? I am new to this, so any comment / criticism I am putting in my mind is welcome.

  #include "stdafx.h" #include & lt; Memory & gt; #include & lt; Vector & gt; # Include & lt; Algorithm & gt; # Include & lt; Iostream & gt; using namespace std; Structure point {int x; Int y; }; Class graphics_surfaces {class drawable_concept {public: virtual zero draw (point const & coordinate) {}; Virtual ~ drawable_concept () {}; }; Template & lt; Class T & gt; Square drawable_model: public drawable_concept {public: drawable_model (T & amp; item): item_ (item) {} draw zero (point const and coordinate) {item_.draw (coordinate); } ~ Drawable_model () {} Private: T item_; }; Public: Template & lt; Class T & gt; Zero Push_back (T and Drawable) {v_.push_back (shared_ptr & quot; drawable_concept & gt; (new drawable_model & lt; T & gt; (drawable)); } Zero draw (point const & coordinate) {for_each (v_.begin (), v_.end (), [& amp;] (shared apt & lieutenant; dracula_konis & gt; and concept) {concept-> Draw (coordination);}); Private: Vector and Lieutenant; Shared _Peter & lt; Drawable_concept & gt; & Gt; V_; }; Structure Triangle {zero point (point const & p) {cout & lt; & Lt; "Triangle:" & lt; & Lt; P. X & Lt; & Lt; "," & Lt; & Lt; P.I. & Lt; & Lt; Endl; }}; Structure Square {zero point (point const & p) {cout & lt; & Lt; "Square:" & lt; & Lt; P. X & Lt; & Lt; "," & Lt; & Lt; P.I. & Lt; & Lt; Endl; }}; Int _tmain (int argc, _TCHAR * argv []) {point p; Px = 1; PI = 2; Graphics_ surface surface; Surface.push_back (triangle ()); Surface.draw (p); Return 0;    

Some points:

  • me under drawable_concept or drawable_model There is no good reason to enter - You prevent reuse in types of containers that are potentially useful in other container types ...

  • < P> You may have some const problems
    • draw might be const (and the function definitions Should not be a semicolon; -)

    • Drawable_model (T & amp; Item) item const

    • push_back (t And colorable) shawl take drawable by const

    • You should use make_shared for exception protection

    • The functionality of the "factory" must definitely be separated from a different function, instead of Buried inside push_back

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -