C version of C++'s virtual functions

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

How can C be used to created a "Vtable" like C++'s virtual functions? I bet
this involves struct.

Lost

Bill
 
E

Emmanuel Delahaye

Bill Cunningham said:
How can C be used to created a "Vtable" like C++'s virtual functions? I bet
this involves struct.

You have to fiddle with pointers to functions.
 
M

Malcolm

Bill Cunningham said:
How can C be used to created a "Vtable" like C++'s virtual functions? > I bet
this involves struct.

typedef struct
{
void (*pt)(void *ptr, double t, double *x, double *y);
double (*len)(void *ptr);
void *ptr;
}LINEINTERFACE;

This is an interface for a 2D line. There are two functions, one to get the
x, y position for a point t (0-1) along the length, the other to get the
length of the line.

Now lets define two lines

typedef struct
{
double x1;
double y1;
double x2;
double y2;
} STRAIGHT;

typedef struct
{
double x[4];
double y[4];
} BEZIER;

Now you can write functions

void getpoint(void *straight, double t, double *x, double *y)
{
STRAIGHT *line = straight;
*x = line->x1 + t * (line->x2 - line->x1);
*y = line->y1 + t * (line->y2 - line->y1);
}

And so on.

You the set up your LINEINTERFACES with these function pointers, plus a
pointer to the line data (either a bezier or a striaght line).

Now we have a client function

void movespaceship(LINEINTERFACE *path)
{
double len;
double t;
double x;
double y;

len = path->len(path->ptr);

for(t=0;t<=1;t += 1/len)
{
path->pt(path->ptr, t, &x, &y);

displayshipat(x,y);
dorestofprogram();
}
}

Note that we can now add any type of line to this function, as long as we
can calculate a length and a point t. The ship could go in a spiral, or a
zigzag, or anything else.
 
M

Malcolm

Bill Cunningham said:
Great. Does the STRAIGHT *line=straight;
Make straight the client through the line pointer to the interface
straight?
That's what it looks like to me. Kind of like the new in C++. That's
what it is doing here right?
In C++ every member function takes a this pointer as its first parameter
(hidden).
When you use virtual functions, a hidden element of the this pointer points
to a table of function pointers.

In C++ we would write

class line
{
public:
virtual void pt(double t, double *x, double *y) = 0;
virtual double len(void) = 0;
};

class straightline : public line
{
// here we would define all the methods
};

Then we would have a spaceship object

class spaceship
{
public:
void move(line *path)
{
double i;
double x;
double y;

for(i=0;i<=path->len();i++)
{
line->pt(i/path->len(), &x, &y);
drawat(x, y);
/* do rest of your display work */
}
}
};

In C, what we are doing is making explicit all the bookkeeping work that C++
does for us automatically. The void * for instance is the this pointer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top