SV: [DGD] #Including code.

Troels Therkelsen troels at 2-10.org
Sat Jan 10 13:17:39 CET 2004


noah_gibbs at yahoo.com writes:
> --- Robert Forshaw <iouswuoibev at hotmail.com> wrote:
> > I'm not talking about what is possible,
> > but what is the 'correct' thing to do.
> 
>   This is like asking about etiquette in using 'goto'.
>  Style nazis (those who set these rules) would
> cheerfully tell you that the only correct thing is not
> to do it.
> 
>   Code in a header file and code in a header directory
> (the two alternatives you mention) are similarly bad. 
> Neither is all that awful, neither is good, neither is
> much worse than the other.
> 
>   If you're *really* worried about precedent, some old
> C++ programs put code in the headers for inline member
> functions.  I suppose that could mean that code in a
> header is tolerable, so maybe you should end your
> filename in .h.
> 
>   But if you're asking what you 'should' do, the
> answer is that you shouldn't.

While Noah is correct that it comes down a lot to personal
preferences, there is something one should be aware about
prototyping object methods in LPC, which differs a lot from
how prototyping works in C.

If you prototype an object method in LPC you are also at
the same time implicitly declaring this method abstract;
that is to say, you are saying that this method could
(or should, if you're a style nazi ;-) be defined in some
other object in the inheritance hierarchy (typically in
a subclass).

This is all fine and well, but it can have an unexpected
(indeed even undesirable) effect.  Take this example
(assume that print() is just a function that outputs to
the console screen):

a.c:
----
  void test_proto();

  void test_proto() {
    print("*** A ***");
  }

  void do_stuff1() {
    test_proto();
  }

b.c:
----
  inherit "a"

  void test_proto() {
    print("*** B ***");
  }

  void do_stuff2() {
    test_proto();
  }

b->do_stuff1() ==> "*** B ***"
b->do_stuff2() ==> "*** B ***"


Gotcha!  Now this implicit abstract declaration of object
methods is sometimes what you want, since it's a way of
saying that you always want the *most* specialised instance
of a method, regardless of where it is defined (it saves you
from typing this_object()->test_proto() in "a").  The only
way to avoid this behaviour is to declare your method
private, which is often not what you want, either.

So, in the words of a style nazi, don't declare prototypes
for object methods unless you want the behaviour outlined
above; instead rearrange them in your source code so you
don't need to use prototyping for scoping reasons.

Regards,

Troels Therkelsen

_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list