c++ - typecasting and check is an instance exists in a vector -
I have a virtual class shape that is left with square rectangle, triangle, divisional size and various characteristics (size, side the number is. I want to store a different size in a structure (let's say a vector).
Then my vector will be: vector & lt; Size * & gt; Myvec;
I would like to know how to find out that a given example of the shape is present in the vector (ex: new circle (diameter 10);
) .
I have heard about dynamic artists, but I do not understand exactly how to use it?
class size {public: shape}} Virtual Fit Size () = 0; Virtual side-side () = 0; Virtual Bull Assemble (Const Shape & amp; rhs) = 0; Integer shape, side; }; Square circle: public size {public: circle (int diameter): size (diameter) {side = 0; } Bool Assemble (Constrict Circle and RA) {Return Sizes == R. s. } Integer size, side; }; Square rectangle: rectangle (rectangle nbsides, integer size 1, integer size 2): side (nbsid), size 1 (size 1), size 2 (size 2) {} bool uneven (chest rectangle and ra) {return (Size 1 = rhs. Size 1 & amp; amp; rhs.size2 == size 2); } Int side, size 1, size 2; };
dynamic_cast
is correct:
Size * size = myvec [0]; Circle * circle = dynamic_cast & lt; Circle * & gt; (Shape); If (circle! = Nullptr); // This is a circle! Do something else; // This is not a circle, do something else. But the best answer to this question is that, in a whole world, you should use polymorphism, sometimes it rarely has to be done. On the additional comments below, I think you want to use dynamic_cast
s in a specific virtual aisle () shape inside a variety of implementations (a polymorphic comparison One of the few places inside the function, I can use the Dynamic_cast
later without feeling the need to wash my hands).
// The code has been edited which is not directly relevant square shape {Public: Virtual Bool Assemble (Const Shape & amp; rhs) = 0; }; Square Circle: Public Size {Public: // Snip Virtual Bull Assemble (Constellation and RA) Override {Circle * rhsAsCircle = dynamic_cast & lt; Circle * & gt; (& Amp; rhs); If (rhsAsCircle == nullptr) false return; // No Circle; Can not resize equals withdrawal == rhsAsCircle-> size; }};
Then somewhere else:
Circle Search Circle (10); For (size * size: myvec) if (size-> uneven (search circle)); // We have a match!
Comments
Post a Comment