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

From: jd (jedrzej_dudkiewicz_at_poczta.interia.pl)
Date: 03/17/04


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



Relevant Pages

  • Re: Mouse?!
    ... left button moves the paddle up and right button moves the paddle ... it can be random or always the player or start the ball ... the serve":) then darts up to the bottom and releases the ball...an ... Though you wouldn't want to actually see the mouse pointer while playing ...
    (alt.lang.asm)
  • sizeof(object) is different in ANSI and Unicode
    ... A class having no member variables and only a method sizeof ... I have the answer for this of how in works in ANSI. ... form a pointer to an empty object that is distinct from a pointer to ... For a class with virtual function, size of a virtual function is a ...
    (comp.lang.cpp)
  • Re: Vista Media Player
    ... Windows Media Player cannot play the file. ... You've encountered error message C00D1199 while using Windows Media ... a little tip on how you can find the mouse pointer easily: ... But it shouldn't hide in wimp. ...
    (microsoft.public.windows.vista.general)
  • Re: Why sizeof(struct) is different in C and C++ ?
    ... >> My conceptual view is a stack of base classes, ... For "ordinary" inheritance where each class inherits from exactly one immediate ... for just a single virtual function table pointer. ...
    (comp.lang.cpp)
  • Re: Would a static_cast be better style here?
    ... >> And of course to convert from a derived pointer type to a base pointer ... > pointer, then you are correct, there is no need to cast. ... For a virtual function the scope resolution operator is the only way. ...
    (comp.lang.cpp)