Re: is it good, is it bad or plain eeevil ?

From: Nick Hounsome (nh002_at_blueyonder.co.uk)
Date: 03/17/04


Date: Wed, 17 Mar 2004 18:47:16 -0000


"jd" <jedrzej_dudkiewicz@poczta.interia.pl> wrote in message
news:c3a3sh$1b8n$1@mamut.aster.pl...
> > > class base {
> > > protected:
> > > virtual void vfun() {
> > > cout << "so much fun in base::vfun() !" << endl;
> > > }
> > > void call_vfun(base& b) {
> > > b.vfun();
> >
> > This is a virtual function call regardless of how call_vfun was called
> hence
> > it will not call
> > base::vfun if b is a derived.
>
> Exactly what I was trying to do.
>
> > In fact this method is pointless because it could be an unrelated free
> > function and
> > it would work exactly the same!
> >
> > > }
> > > public:
> > > virtual void fun(base& b) { cout << "simple fun in base::fun()."
<<
> >
> > What would you have this do in a real prog?
> >
> > > virtual void fun(base& b) {
> > > cout << "deriv::fun(): "; base::call_vfun(b);
> >
> > Would work just as well without base:: as call_vfun is not virtual.
>
> Right, my "mistake".
>
> > > int main(void)
> > > {
> > > deriv d;
> > > base b;
> >
> > What is b for???
> >
> > > d.fun(d);
> > should give:
> > there could be fun in deriv::vfun() !
>
> Right. In fact, base b left from previous version:
> b.fun(b);
> b.fun(d);
> d.fun(b);
>
> > > return 0;
> > > }
> > >
> > > </code>
> > >
> > >
> >
> > Does anybody even bother trying their code before posting?
>
> Yes, I do. It works the way I want it to work.
>
> You asked what would I used for in a real prog. Well, I'll answer, as I
want
> answer to my question. I'm writing <dadam!> mud (multi user dungeon), a
text
> based online multiplayer game </dadam!>. For this purpose I've created
base
> class called Object (damn, how smart), which is a base class for all
classes
> that represent things in game (weapons, armours, paper, weeds, trees and
> also players, monsters, even locations that you can enter. Object itself
is
> a container, so objects can be easily nested. So I want this construction
to
> work like this:
>
> player->addObj(sword); // valid
> sword->addObj(player); // also valid
>
> If sword is 'given' to player (that is pointer is passed), addObj check
few
> conditions and calls Object's method assign(), that moves sword from it's
> previous location (it has to pointer to a container that it is currently)
to
> player, simply changing some pointers. assign() is protected AND can work
> differently for different classes (it isn't always swapping same pointers,
> some objects do other things while assign()'ing), thus we have protected,
> virtual method. And this is the way I resolved calling it. I don't have
any
> working code, as whole thing exists mainly on paper (you know, white
pieces
> of processed wood or hemp that lay everywhere and appear out of nowhere),
> this is only a check how I can do it. Hope this explains some things, and
I
> hope now you can answer my question.
>
> Thanks for attention and, possibly, answer.
>
> JD

I'm still not really clear what you are trying to do but when I last saw
something like this
it was in hack and they used a matrix - this may well be the best approach
if you have a lot of classes.
You could store pointers to member functions.

The cleanest approach will probably be double dispatch - it does couple
things together but is very elegant
provided the number of classes is small.
If you are not familiar with it the basic idea is that you use a virtual
function call to get to the most derived class of one object
and overloading to then get to a method where the full type of both
participants in the operation is known:

i.e.

have base methods
virtual void add(Object& o);
virtual void doAdd(Sword& s);
virtual void doAdd(Player& p);
etc

every class implements add as
void XXX::add(Object& o) { o.doAdd(*this); }
causing the right virtual overload to be called
Then all the doAdd methods know exactly what type they are and what type the
other thing is.
By using intermediate classes such as Weapon you don't need to override
absolutely everything.



Relevant Pages

  • c++ : a baseclass, derived classes, a list of baseclass pointers, a problem.
    ... virtual void func1 ... Now i created a list of pointers to insances of the baseclass ... How can I execute the right function? ... I use a pointer to the baseclass to loop trough the list. ...
    (comp.programming)
  • Re: RunModalLoop and destructor
    ... Implement the virtual void OnCancel(); and virtual void OnOK; ... thr framework when the call to the modal dialog returns. ... > the whole program i would like to call delete on these pointers. ... > Unfortunately DestroyWindow() isn't called when the dialog is ...
    (microsoft.public.vc.mfc)