Solved

Dummy Funtcions

Posted on 2004-04-04
4
272 Views
Last Modified: 2010-04-15
Please explain to me what is a dummy function, what is it useful for and what the difference between the other methods (if there are, what are they?)
0
Comment
Question by:thekng
4 Comments
 
LVL 2

Expert Comment

by:timbauer
ID: 10753845
Not sure what you mean about a "dummy function".
A macro is a pure text substitution done by the preprocessor, but kind looks like a function.

Can you rephrase are give more of the context where you heard of a dummy function?
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 50 total points
ID: 10754388
Hi thekng,

A dummy function is usually just an empty function.  At first glance it might seem silly to call such a function, but it can actually make sense in some circumstances.

Function tables are one place where you're likely to see dummy functions.  The following table could be used to process characters based on the characters value.  


int (*ExecuteFunction[256])(void) = {NULL, NULL, NULL, ...,  IsA, IsB, IsC, ...};

  if (ExecuteFunction[Character])
    ExecuteFunction[Character] (ParameterList);


int (*ExecuteFunction[256])(void) = {Ignore, Ignore, Ignore, ...,  IsA, IsB, IsC, ...};

  ExecuteFunction[Character] (ParameterList);


In the first example, ExecuteFunction[Character] must be explicitly checked to make sure that there is a function to execute.  The second example inserts a dummy function (Ignore) into the table.



Hope this helps,
Kent
0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 25 total points
ID: 10755504
Hi thekng,
Besides the function table use that Kent already mentioned, dummy functions are often used in callback scenarios, such as GUI events. Unlike in Kent's example, in such an environment the API user doesn't have a choice of different implementations, since the calling code is not his (it's the OS/GUI/class lib, etc).


Imagine a situation like this:

struct AllPossibleThingsAUserCanDo{
    event_ptr key_pressed;
    event_ptr left_mouse_click;
    event_ptr right_mouse_click;
    event_ptr window_resized;
    [...]
};

Now you want to ignore events -> you need a dummy handler.

Dummy methods (as opposed to functions) are often used in C++ class hierarchies to have a pre-defined interface where some parts of the classes implement some methods as ... dummies.

Cheers,
Stefan
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 100 total points
ID: 10755659
As Kent suggests, the most common use of dummies is in tables of functions. Stephan's callbacks are also common uses, but this is more usually a function pointer rather than a function itself. E.G.:

// Callback before each expansion.
char * (*Expanding) (char * before,char * after) = NULL;

...

if ( Expanding != NULL ) (*Expanding) ( before, after );

A third use is for what I call link-time coding:

void Expanding ( char * before, char * after ) {};

...

Expanding ( before, after );

...

You can then put this code in a library and, with careful use of link parameters, you can write your own 'Expanding' without modifying the library code.



0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question