Overloading C-functions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Scalpel

Overloading C-functions

Post by Scalpel »

I'm writing a few functions in C for my os, and need the overloading functionality that C++ offers.

My current system is written in a mishmash of C and C++., and now want to rewrite everything into C.

For instance my clear_screen function looks somewhat like this:

Code: Select all

void clear_screen(int colors=0)
{
(...a little code...)
}
where it can be called with any color, but will default to black if no color is given.

How can I achieve this in regular C?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Overloading C-functions

Post by Pype.Clicker »

There is no real "overloading" in C. One classical approach is to use zero values (or null pointer) to request the default behaviour.

For instance:

Code: Select all

file_open(directory *dir, char *filename, char *mode)
{
     dir = dir?:RootDirectory;
     mode = mode?:"r";
}
and you call it with

Code: Select all

file_open(NULL,"/home/my_file",NULL);
file_open(ADirectory,"file_in_dir","rw+");
Another option (when you have plenty of parameters to give), is to declare a parameter structure and a "presence" flag, for instance

Code: Select all

text_render("Hello",TT_FONT|TT_COLOR|TT_STYLE,
    (text_msg){font:"myFont.ttf", color:red, style:TEXT_BOLD});

text_render(char *txt, enum TT_OPTIONS opt, text_msg m) {
    if (!(opt&TT_FONT)) m.font=DefaultFont;
    if (!(opt&TT_COLOR)) m.color=black;
    if (!(opt&TT_STYLE)) m.style=TEXT_REGULAR;
 ...
}
Scalpel

Re:Overloading C-functions

Post by Scalpel »

Hmm...ok...

So you just call the function with NULL instead. Where do you set the default values? Is that what this does??

Code: Select all

dir = dir?:RootDirectory;
mode = mode?:"r";


So my code would look something like this?

Code: Select all

void clear_screen(int color)
{
(some code for setting color to black if color = 0)
(+ rest of code)
}
and called with:

Code: Select all

clear_screen(NULL);
when I want the default black background?
Whatever5k

Re:Overloading C-functions

Post by Whatever5k »

Yes, that's what pype's code was doing:

Code: Select all

foo = expr ? (if expr is true) : (if not true)
Scalpel

Re:Overloading C-functions

Post by Scalpel »

Ok, now things make a little more sense. Now, If I could show you one of my functions.

Code: Select all

void iprint(int number, char base)
{
        base = base?:'b';
        int number_base;
        char string[] = "UNDEFINED";                                    //created a tripple error in bochs when defined as ""
        switch (base)
        {
                case 'h':
                        number_base = 16;
                        break;
                case 'b':
                        number_base = 2;
                        break;
                case 'd':
//              case '' :
                        number_base = 10;
                        break;
                default:
                        print ("error: print: base not correct!", FONT_ERROR, global_cursor);


        }

        convert_int_to_string(number, string, number_base);
        print(string,FONT,global_cursor);
};
when I call this code with:

Code: Select all

int testvalue = 2;
iprint (testvalue,NULL);
I get errors during compiling. I've just been writing C a few days, so there is probably some bad code here somewhere...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Overloading C-functions

Post by Pype.Clicker »

NULL only makes sense when you're dealing with pointers. For integers/long/short/char etc. you must use 0 instead.

iprint(a_number,0);

btw. convert_int_to_string() made a tripple fault when string[]="" because you didn't allocated enough space for what you are storing.

use

Code: Select all

char string[N_DIGITS_IN_LONGEST_NUMBER_EVER+1];
instead.

(+1 is for terminating '\0' character, of course ;) )
Scalpel

Re:Overloading C-functions

Post by Scalpel »

Thank you guys! I'll do some testing-and-failing-and-testing-again now.

You've made a Norwegian with the flu smile today :D
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Overloading C-functions

Post by Pype.Clicker »

Scalpel wrote: Thank you guys! I'll do some testing-and-failing-and-testing-again now.

You've made a Norwegian with the flu smile today :D
lol. Where do we sign the Guiness Books ?
Post Reply