www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/24/10:49:09

From: "James E Fisher" <James DOT Fisher AT team17 DOT com>
Newsgroups: comp.lang.c++,comp.os.msdos.djgpp,rec.games.programmer
Subject: Re: pointer to function ??
Date: 24 Feb 1998 09:36:51 GMT
Organization: Team 17 Software Ltd
Message-ID: <01bd4107$bba0dc60$77111111@jfisher.team17.com>
References: <6c7ltq$ms1$1 AT o DOT online DOT no> <6c7u5l$mub$2 AT news DOT ox DOT ac DOT uk>
NNTP-Posting-Host: gate-isdn.team17.com
Lines: 35
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Thomas Womack <mert0236 AT sable DOT ox DOT ac DOT uk> wrote in article
<6c7u5l$mub$2 AT news DOT ox DOT ac DOT uk>...
> [iC] (jan AT narvik DOT crosswinds DOT net) wrote:

[snip]

> Wouldn't it be easier, and not discernably slower, to put
> 
> int nextthink;
> 
> nextthink=THINKER_RUN;
> nextthink=THINKER_ATTACK;
> 
> switch nextthink
> {
>   case THINKER_RUN:
>     run();
> 
>   case THINKER_ATTACK:
>     attack();
> }
> 
> Tom

The first idea of a pointer to the code is the fastest way as there is only
one call to the required function (after the address of the function has
been fetched).  Using a SWITCH statement is slower as it can end up as a
list of IF() statements by the compiler.

You also need to think about debugging.  Using the pointer to a function is
much cleaner to debug than having to wade through switch statements.
--
James E Fisher, C++ Tools Programmer, Team 17 Software Ltd
http://www.team17.com http://www.geocities.com/SiliconValley/Lakes/5639

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019