Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Certainly you can use virtual methods/late binding when you only care about the type of one object, but when you care about more than one, it's not so easy. Here's a better example:

Okay. It took a while to drag the steps back from my memories of a former life, and I have a feeling that a few of these steps could be eliminated, but try this:

#include <iostream.h> class Circle; class Polygon; class Shape { public: virtual ~Shape(){}; virtual void ShowType() { cout << "Shape" << endl; } virtual void intersect( Shape *that ) { cout << "Shape:intersect" << endl; } virtual void intersect_( Circle * ); virtual void intersect_( Polygon * ); }; class Polygon : public Shape { public: virtual void ShowType() { cout << "Polygon" << endl; } virtual void intersect_( Polygon *that ) { that->intersect( this ) +; } virtual void intersect( Polygon *that ) { cout << "Intersect Polygon with Polygon" << endl; } void Polygon::intersect_( Circle *that ); virtual void intersect( Shape *that ) { that->intersect_( this ); +} virtual void intersect( Circle *that ) { cout << "Intersect Polygon with Circle" << endl; } }; void Shape::intersect_( Polygon * ) { } class Circle : public Shape { public: virtual void ShowType() { cout << "Circle" << endl; } virtual void intersect_( Circle *that ) { that->intersect( this ); + } virtual void intersect( Circle *that ) { cout << "Intersect Circle with Circle" << endl; } virtual void intersect_( Polygon *that ) { that->intersect( this ) +; } virtual void intersect( Polygon *that ) { cout << "Intersect Circle with Polygon" << endl; } virtual void intersect( Shape *that ) { that->intersect_( this ); +} }; void Shape::intersect_( Circle * ) { } void Polygon::intersect_( Circle *that ) { that->intersect( this ); } int main() { Shape *circle = new Circle; circle->ShowType(); Shape *polygon = new Polygon; polygon->ShowType(); circle->intersect( polygon ); circle->intersect( circle ); polygon->intersect( circle ); polygon->intersect( polygon ); return 1; }

Outputs:

c:\test>696596 Circle Polygon Intersect Circle with Polygon Intersect Circle with Circle Intersect Polygon with Circle Intersect Polygon with Polygon

This evolved in the days before RTTI. Whether I'd use it now in preference to RTTI is debateable. I guess it would depend upon the scale of the project and the affects of enabling RTTI. If it could be localised to some small part of a large project, it might be worth the costs.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^6: Runtime introspection: What good is it? by BrowserUk
in thread Runtime introspection: What good is it? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-25 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found