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 6 of 7 Topic 45819 of 48020
Post > Topic >>

Re: Double dispatch (makes no sense!)

by James Kanze <james.kanze@[EMAIL PROTECTED] > May 9, 2008 at 06:16 AM

On May 9, 10:42 am, saneman <d...@[EMAIL PROTECTED]
> wrote:
> hurcan solter wrote:
> thanks! Yes it was a bit late :-) I am wondering why the baseclass
> should be declared as abstract.

It doesn't have to be.

> I have tried to change it to:
>
> class geometry_type
> {
> public:

>    virtual void haps(geometry_type* other)
>    {
>           other->collideWith(this);

And what should this call?  Function overload resolution only
concerns static types.

>    }

>    virtual void collideWith(Box* other)
>    {
>      std::cout << "Box collision" << std::endl;
>    }

>    virtual void collideWith(Sphere* other)
>    {
>      std::cout << "Sphere collision" << std::endl;
>    }

> //  virtual void haps(geometry_type* other)=3D0;
> //  virtual void collideWith(Box* other)=3D0;
> //  virtual void collideWith(Sphere* other) =3D 0;
> };

> But then I get:
>
> test2.cpp: In member function ?virtual void
> geometry_type::haps(geometry_type*)?:
> test2.cpp:16: error: no matching function for call to
> ?geometry_type::collideWith(geometry_type* const)?
> test2.cpp:19: note: candidates are: virtual void
> geometry_type::collideWith(Box*)
> test2.cpp:24: note:                 virtual void
> geometry_type::collideWith(Sphere*)

> Is there some rule that the baseclass must be abstract?

No.  But there is a rule that when you call a function, one of
the function found with name lookup must be callable with the
static types of your arguments.

Think about it for a moment.  What you're asking for is
basically double dispatch.  Or at least, dispatch on the dynamic
type of an argument, not on the object the function is called
on.

--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 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 Tue Oct 14 8:46:17 CDT 2008.