Re: is it good, is it bad or plain eeevil ?
From: jd (jedrzej_dudkiewicz_at_poczta.interia.pl)
Date: 03/17/04
- Next message: Kevin Goodsell: "Re: Why does this function produce a much larger, empty text file?"
- Previous message: Michael Harris: "Re: arbitrary timezone conversion routines"
- In reply to: Nick Hounsome: "Re: is it good, is it bad or plain eeevil ?"
- Next in thread: Nick Hounsome: "Re: is it good, is it bad or plain eeevil ?"
- Reply: Nick Hounsome: "Re: is it good, is it bad or plain eeevil ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 17 Mar 2004 19:05:24 +0100
> > 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
- Next message: Kevin Goodsell: "Re: Why does this function produce a much larger, empty text file?"
- Previous message: Michael Harris: "Re: arbitrary timezone conversion routines"
- In reply to: Nick Hounsome: "Re: is it good, is it bad or plain eeevil ?"
- Next in thread: Nick Hounsome: "Re: is it good, is it bad or plain eeevil ?"
- Reply: Nick Hounsome: "Re: is it good, is it bad or plain eeevil ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|