473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C version of C++'s virtual functions

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

Lost

Bill

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
Nov 13 '05 #1
3 2488
In 'comp.lang.c', "Bill Cunningham" <so**@some.net> wrote:
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.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #2

"Bill Cunningham" <so**@some.net> wrote in message
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.
Nov 13 '05 #3

"Bill Cunningham" <so**@some.net> wrote in message
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.
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
5
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new...
18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
7
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
5
by: alex goldman | last post by:
When compiling this code with g++ -Wall -pedantic -ansi -c program.cpp # this is GCC-3.3.4 ###################################### #include <vector> class b { public: virtual void f() = 0;...
7
by: ashishnh33 | last post by:
i want to know about the concept behind the virtual functions.
5
by: Dilip | last post by:
In a polymorphic hierarchy is it common practice to have public virtual functions in the base class with a default empty implementation and let the derived classes decide whether they want to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.