LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2004, 10:53 AM   #1
ypzhuang
LQ Newbie
 
Registered: Jul 2004
Location: Shanghai,China
Posts: 7

Rep: Reputation: 0
hot ot overload a function in c?


I have found that functions can't be overloaded in c,but it is easy in C++ or Java.Shall we have a way to overload a function ?
Have you a document about how to use object-oriented technology with c?
Thanks a lot!!!
 
Old 08-11-2004, 11:08 AM   #2
awillard69
LQ Newbie
 
Registered: Aug 2004
Location: Chicagoland
Distribution: Fedora
Posts: 13

Rep: Reputation: 0
You are correct, C is not object oriented and therefore does not support function overloading.

If you really need a work around, consider a wrapper type function that accepts a parameter list, much like printf(). Make your determinations of which parameters are passed and invoke the appropriate function and return its value.

But, that's a lot of work. If you want to use objects, overloading, etc, use C++.
 
Old 08-11-2004, 11:13 AM   #3
leokor
LQ Newbie
 
Registered: Aug 2004
Location: Brookline, MA, USA
Posts: 8

Rep: Reputation: 0
I'm not sure I understand your question. C is not an object-oriented language. Developing a new object-oriented language based on C is moot, because there is C++ already.

Note that it is possible, of course, for a group of developers to agree on certain constraints when developing a software module in C, that would effectively amount to using an object-oriented approach. But since those constraints are not enforced by the compiler, there is always a possibility of someone breaking them at will. Such modules can be difficult to maintain, especially if the previous developers leave and the new ones come aboard, but it can be done.

An historical example of such approach is, I believe (please correct me if I'm wrong), the X widgets developed at MIT. I have also known someone who once used a species of home-baked "object-oriented" approach when coding in assembler.

But the point is that the need for anything of the sort has been obviated long ago, when C++ and Java entered the scene.

Leo
 
Old 08-11-2004, 11:56 AM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
A typical way to do objects in C could be

Code:
struct dog_functions {
    void (*bark)(struct dog* this);
    void (*pee)(struct dog* this, struct firehydrant* target);
    void* data;
};

struct dog {
    struct dog_functions* fns;
    int feet;
    int age;
    const char* name;
    void* data;
}
So if you have a struct dog, *fido, you can say fido->fns->bark(fido); or fido->fns->pee(fido, my_firehydrant);
You can accomplish polymorphism by modifying the dog_functions struct attached to fido.

A real world example of this is the VFS in the linux kernel. I've tried to more or less follow its layout here.

The reason dog_functions is broken out into a separate struct is so you can save space by having multiple dogs point to the same dog_functions struct.

There's not a real great mechanism for inheritance here, I can show you one for single inheritance if you want -- there's a gobject library that does this. All the documentation for gobject, glib, and gtk will give you some idea of how OO works in C.

"Overloaded" C functions generally tend to look like void foo_int(int a), void foo_char(char c), void foo_double(double d), etc. i.e., they aren't overloaded functions : ) The varargs path is one of last resort, but it's there too.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Overload brentos Linux - Software 7 07-20-2005 06:34 AM
fork() overload ziggyboy Linux - Security 1 09-04-2004 06:39 PM
cpu overload webwolf70 Linux - Hardware 2 03-22-2004 12:59 PM
redhat 8.0 overload??? frik Linux - Hardware 15 01-07-2004 02:20 PM
how to overload operator! jhorvath Programming 2 10-31-2003 07:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration