www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/03/14/15:24:06

From: "Traveler" <traveler AT netti DOT fi>
Newsgroups: comp.os.msdos.djgpp
Subject: TABLE OF POINTERS TO FUNCTIONS + OTHER
Date: Tue, 14 Mar 2000 21:45:12 +0200
Organization: SAUNALAHDEN SERVERIN asiakas
Lines: 87
Message-ID: <8am435$j8i$1@tron.sci.fi>
NNTP-Posting-Host: mdxl.tdyn.saunalahti.fi
X-Trace: tron.sci.fi 953062309 19730 195.197.83.140 (14 Mar 2000 19:31:49 GMT)
X-Complaints-To: newsmaster AT saunalahti DOT fi
NNTP-Posting-Date: 14 Mar 2000 19:31:49 GMT
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hello to everyone !
(This is going to be long... Sorry !
 traveler AT netti DOT fi)


I tried to make a table that can store pointers to any kind of functions
like the one below.

void     func(void);
int        func2(int,int);
long     func3(int);

void*    table[ ] = {func,func2,func3};

To my delight the compiler didn´t complain but when I actually tried to use
one:

table[2](5,6);

It generated the message "Error: 'table[2]' cannot be used as a function"
I suspect that this is becourse the table is actually a constant pointer.

So, what do I have to do to make a table of varied function pointers ?

Build my own table class that dynamically allocates memory and accepts any
kind of data ?
Possible use of templates ?

Any hints and tips please !

BONUS QUESTION :

When building my new class I have always hated making new version of access
functions to the various class members.
Example (trivial parts for this example left out...):

typedef    unsigned char    BYTE;
typedef unsigned long DWORD;

class    Circle
{
public:
    int    getx(void)const {return x;}                            // No
sweat...
    int    gety(void)const {return y;}                           // Urgh !
    BYTE    getcolor(void)const {return color;}        // Another one ??
    DWORD getradius(void)const {return radius};    // I really hate this !!!
private:
    int    x;
    int    y;
    DWORD    radius;
    BYTE    color;
};

If this all could be done with say, a single overloaded operator then life
would be much easier !
I had following in mind:

// enum {X,Y,RADIUS,COLOR};    No, this won´t work becourse if I make
another class like 'POINT' then
// the 'index' COLOR will be 3 not 4. I think that I must make a enum that
is PART of the class...

class    Circle
{
public:
   template <class T>    T    operator()(const int index)const {return
table[index];}
    enum {X,Y,RADIUS,COLOR};
private:
    int    table[4];        // Otherwise nice but I think I must make my own
table class
};

Usage:
    Circle    circle(160,100,10,BLUE);
    cout << circle(CIRCLE::COLOR) << endl;        // Not an elegant at all.
If I just could use the calls like

// circle(COLOR), rectangle(COLOR), etc...

If anybody has a way to make this easier then please tell me.

Stefan Fröberg
"Just a Traveler..."


- Raw text -


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