Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Double disp...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 7 Topic 45819 of 47034
Post > Topic >>

Re: Double dispatch (makes no sense!)

by Andrey Tarasevich <andreytarasevich@[EMAIL PROTECTED] > May 8, 2008 at 05:46 PM

saneman wrote:
> ...
> class Box : public geometry_type
> {
> public :
>   Box() {}
> 
>   virtual void haps(geometry_type* other)
>   {
>         other->collideWith(this);
>   }
> 
>   virtual void collideWith(Sphere* other) {
>     std::cout << "Sphere collision with Sphere" << std::endl;

Huh? This should be "Box collision with Sphere", shouldn't it?

>   }
>
>   virtual void collideWith(Box* other) {
>     std::cout << "Box collision with Box" << std::endl;
>   }
> 
> 
> };
> 
> class Sphere : public geometry_type
> {
> public :
>   Sphere() {}
> 
>   virtual void haps(geometry_type* other)
>   {
>         other->collideWith(this);
>   }
> 
>   virtual void collideWith(Sphere* other) {
>     std::cout << "Sphere collision with Sphere" << std::endl;
>   }
> 
>   virtual void collideWith(Box* other) {
>     std::cout << "Sphere collision with Box" << std::endl;
>   }
> 
> 
> };
> 
> 
> void narrow_phase(geometry_type* G1, geometry_type* G2)
> {
>   G1->haps(G2);
> }
> 
> int main()
> {
>   Box* BoxA       = new Box();
>   Sphere* SphereA = new Sphere();
> 
>   BoxA->collideWith(BoxA);
>   SphereA->collideWith(BoxA);
> 
>   // Should print box collide with box.
>   narrow_phase(BoxA, BoxA);
> 
>   // Should print sphere collide with box.
>   narrow_phase(SphereA, BoxA);

No. Your 'haps' method essentially "swaps" the objects involved in the 
collision (take a closer look at it): 'G1' from 'narrow_phase' becomes 
'this' in 'haps'. So, it should print "Box collision with Sphere". And 
it will, if you fix the error mentioned above.

>   // Should print box collide with sphere.
>   narrow_phase(BoxA, SphereA);

Again, no. For the very same reason it should print "Sphere collision 
with Box". And that exactly what it prints.

>   return 0;
> 
> }
> 
> 
> But when I run main I get:
> 
> Box collision with Box
> Sphere collision with Box
> Box collision with Box
> Sphere collision with Sphere
> Sphere collision with Box
> 
> 
> What goes on in the last two calls?

See above.

-- 
Best regards,
Andrey Tarasevich
 




 7 Posts in Topic:
Double dispatch (makes no sense!)
saneman <ddd@[EMAIL PR  2008-05-09 02:20:57 
Re: Double dispatch (makes no sense!)
Andrey Tarasevich <and  2008-05-08 17:46:12 
Re: Double dispatch (makes no sense!)
"Victor Bazarov"  2008-05-08 20:48:34 
Re: Double dispatch (makes no sense!)
hurcan solter <hsolter  2008-05-09 03:49:57 
Re: Double dispatch (makes no sense!)
saneman <ddd@[EMAIL PR  2008-05-09 10:42:44 
Re: Double dispatch (makes no sense!)
James Kanze <james.kan  2008-05-09 06:16:31 
Re: Double dispatch (makes no sense!)
Daniel Pitts <newsgrou  2008-05-08 18:33:47 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:01:46 CDT 2008.