LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What is function overriding in C (https://www.linuxquestions.org/questions/programming-9/what-is-function-overriding-in-c-300383/)

skie_knite007 03-11-2005 08:03 AM

What is function overriding in C
 
What may I kno,is meant by function overriding

itsme86 03-11-2005 08:13 AM

There is no function overriding in C, only in C++. Try a google search on C++ function overriding and you'll find more information than you need.

TheLinuxDuck 03-11-2005 08:28 AM

In a language that support overriding (which C does not), this usually means that you can redefine an existing function, exactly as it is declared, and define it as you like. For example, let's say that language X defines the print function as:
Code:

# ficticious language X print function
int print(va_arg_list) {
  loop_through(va_arg_list) {
    stdout.write(va_arg_list.loopitem);
  }
}

You could potentially rewrite the function, something like:
Code:

int print:override(va_arg_list) {
  newFile = file.open("/log/printlog", "a+t");
  loop_through(va_arg_list) {
    newFile.write(va_arg_list.loopitem);
  }
  newFile.close;
}

This would cause your existing code to still work fine, but would simply allow you to redirect it's output easily.

Hope this helps!

Hko 03-11-2005 09:01 AM

About function overriding in C.
I also thought that that's not possible, but then, how come this is possible?
Code:

shell$ man fcntl

[...]

SYNOPSIS
      #include <unistd.h>
      #include <fcntl.h>

      int fcntl(int fd, int cmd);
      int fcntl(int fd, int cmd, long arg);
      int fcntl(int fd, int cmd, struct flock *lock);

E.G. 3 different prototypes for fcntl(), and all of them work.

To me, this looks like a (limited?) form of function overriding (in C).
I've been wondering before how this is achieved. Anyone?

itsme86 03-11-2005 09:38 AM

Because C doesn't require that you pass all parameters to the function if you leave the parameter list blank in the prototype. The compiler should only throw up warnings if the prototype has a non-empty parameter list and you don't pass enough enough arguments to the function.
Code:

itsme@dreams:~/C$ cat param.c
#include <stdio.h>

void func();

int main(void)
{
  func(1);
  func(17, 5);

  return 0;
}

void func(int a, int b)
{
  if(a == 1)
    puts("Only going to use first argument.");
  else
    printf("Using both arguments: a = %d, b = %d\n", a, b);
}
itsme@dreams:~/C$ gcc -Wall -ansi -pedantic param.c -o param
itsme@dreams:~/C$ ./param
Only going to use first argument.
Using both arguments: a = 17, b = 5

I'm not sure that's how fcntl() does it, but that's one way.

zeropash 03-11-2005 10:07 AM

actually fcnt is defined in the header file using
int fcntl (int fd, int cmd, ...);
This means it can take variable number of arguments.
now it looks at the command and then decides if anything follows as the third parameter or not.
see man va_arg etc for more details.

rose_bud4201 03-11-2005 10:45 AM

Please don't post this in two places, even if it is under another username....

http://www.linuxquestions.org/questi...hreadid=300378

TheLinuxDuck 03-11-2005 11:35 AM

Quote:

Originally posted by Hko
About function overriding in C.
I also thought that that's not possible, but then, how come this is possible?
Code:

      #include <unistd.h>
      #include <fcntl.h>

      int fcntl(int fd, int cmd);
      int fcntl(int fd, int cmd, long arg);
      int fcntl(int fd, int cmd, struct flock *lock);

E.G. 3 different prototypes for fcntl(), and all of them work.

To me, this looks like a (limited?) form of function overriding (in C).
I've been wondering before how this is achieved. Anyone?

Overloading, actually. Overriding is when you modify the behavior of an existing function. Overloading is when you have one function name that is defined with different parameter options. As zeropash mentioned, fcntl is defined with a variable argument list. This is similar to overloading, but not really the same thing. C does not support overriding or overloading, however.


All times are GMT -5. The time now is 01:21 AM.