473,889 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I overload functions in C?

I know that C++ lets you overload functions, but how do I overload functions in C?
Nov 13 '05
13 44219
On 2003-10-13, some one <fr********@yah oo.com> wrote:
I know that C++ lets you overload functions, but how do I overload
functions in C?

You cannot do so arbitrarily, all C solutions require some form of
trickery. Several have been offered to you.

One trickery not yet suggested is to employ a polymorphic interface.

typedef struct polymorph polymorph;
struct polymorph {
void (* const foo)(polymorph *);

void foo(polymorph *p) { p->foo(p); }
Now, you get a form of function overloading when you create instances
of polymorph.

struct A {
polymorph interface;
int a;

static void A_foo(polymorph *p) {
struct A *me = (void *)p;
printf("a:%d\n" , a++);

static const polymorph A_interface = { A_foo };

polymorph * create_A(void) {
struct A *i = malloc(sizeof(s truct A));
i->interface = A_interface;
i->a = 0;

struct B {
polymorph interface;
int b;

static void B_foo(polymorph *p) {
struct B *me = (void *)p;
printf("b:%d\n" , b--);

static const polymorph B_interface = { B_foo };

polymorph * create_B(void) {
struct B *i = malloc(sizeof(s truct B));
i->interface = B_interface;
i->b = 0;

Now, you can get a form of overloading with the foo() function.

polymorph *a = create_A(); /* a points to a struct A */
polymorph *b = create_B(); /* b points to a struct B */

foo(a); /* will output "a:0" */
foo(b); /* will output "b:0" */
foo(a); /* will output "a:1" */
foo(b); /* will output "b:-1" */

-- James
Nov 13 '05 #11
jacob navia wrote:
With lcc-win32 you write:

[stuff that isn't C and isn't topical in comp.lang.c]

Martin Ambuhl

Nov 13 '05 #12

"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message
news:Pi******** *************** ************@un ix46.andrew.cmu .edu...

On Mon, 13 Oct 2003, jacob navia wrote:

"some one" <fr********@yah oo.com> wrote [...]
I know that C++ lets you overload functions, but how do
I overload functions in C?

With lcc-win32 you write:
int overloaded fn(struct a*arg);
int overloaded fn(struct b *arg);

and when you pass to fn an "a" it will call the first, and when you
pass it the second it will call the second.


That's very interesting, Jacob, but the OP did ask about
the C language, not about lcc-win32.

Oh, and *please* don't top-post.


OK OK True, this is an lcc-win32 evil extension...
Sorry, I couldn't resist :-)
If this question arises it is because people need that isn't it?
Nov 13 '05 #13
"jacob navia" <ja*********@ja cob.remcomp.fr> wrote in message news:<bm******* ***@news-reader4.wanadoo .fr>...
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message
news:Pi******** *************** ************@un ix46.andrew.cmu .edu...

That's very interesting, Jacob, but the OP did ask about
the C language, not about lcc-win32.

Oh, and *please* don't top-post.

OK OK True, this is an lcc-win32 evil extension...

Not evil, just off-topic. It isn't polite to post things outside of
the topic in a newsgroup, especially a technical one.
If this question arises it is because people need that isn't it?

I've never needed it, and if I did I'd probably change languages.
(Although the idea of a variadic with the first argument declaring
type is neat...) But needs aren't really here or there when it comes
to topicality on this newsgroup. The topic is Standard C and/or
pre-Standard K&R C, and neither languages has any concept of function
overloading or polymorphism or inheritance or anything else of the

And, good job with curing yourself of top-posting! It's appreciated,
and makes people more likely to regard you as clueful.
Nov 13 '05 #14

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

Similar topics

by: David Sachs | last post by:
The following program illustrates an interesting effect of the way C++ resolves function overloading. I have verified with a member of the C++ stardard committee that the output shown is correct. *********************************Program******************************** #include <iostream> using std::cout;
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; } This overload work but I have a curiosty If I try to approch to the name or to the surname or to the dateofbirth in this way i receive error: ostream & operator<<(ostream &out, Person &p){ out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this least operator for the class Person, but I don't know how write the overload for a class that derived from the class Person. The overload of << in Person is this: ostream & operator<<(ostream &out, const Persona &p){ out<<p.getNome()<<"...
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement as a char rather than incorrectly representing it as an int. I've done this because I want to develop the class so that it will be able to convert between values, ie if I add 500 m to 1 km I'd like a correct result given in metres (1500 m in this...
by: Arturo Cuebas | last post by:
The program below contains a compile error. Following the program you will find the typical fix then my idea for a library that facilitates a more elegant fix. #include <boost\bind.hpp> using namespace boost; struct C {
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
by: sebastian | last post by:
I've simplified the situation quite a bit, but essentially I have something like this: struct foo { }; void bar( foo const & lhs, foo const & rhs )
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar explicitly takes a pointer-to-member with no parameters, why is the lookup for the overloaded function not able to use the number of arguments to determine the appropriate function? Is there a relevant portion of the standard that governs the...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.