LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-11-2005, 09:03 AM   #1
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
What is function overriding in C


What may I kno,is meant by function overriding
 
Old 03-11-2005, 09:13 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 03-11-2005, 09:28 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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!
 
Old 03-11-2005, 10:01 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
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?

Last edited by Hko; 03-11-2005 at 10:04 AM.
 
Old 03-11-2005, 10:38 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 03-11-2005, 11:07 AM   #6
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
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.
 
Old 03-11-2005, 11:45 AM   #7
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

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

http://www.linuxquestions.org/questi...hreadid=300378
 
Old 03-11-2005, 12:35 PM   #8
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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.
 
  


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
What is function overriding in C skie_knite007 Programming 2 03-11-2005 01:29 PM
I need help with Grup (Core overriding Project's) Aziz Linux - Software 1 02-27-2005 01:06 AM
LiLo isn't working... Grub is overriding... totalshredder Linux - General 3 02-25-2005 11:39 PM
Problems overriding mandrake Kensai Slackware - Installation 6 03-04-2004 10:28 PM
mozilla mail - overriding inbox 0x4B Linux - Software 2 07-10-2003 04:45 PM

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

All times are GMT -5. The time now is 12:23 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration