• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Overriding Function in C

Is there a way to have an overriding function in C?
I want to have one function that has parameters (int x, int y, int attr), and one function that has the same name with parameters (int x, int y, int sx, int sy).
1 Solution
No, it is not possible. That works only in C++. However, since C++ is downwards compatible to C in every respect (almost), how about simply compiling under C++, if it's so important for you to overload?
There will be no way in C to override functions. One way to simulate this is

struct my_struct1 {
      int x;
      int y;
      int attr;
struct my_struct2 {
      int x;
      int y;
      int sx;
      int sy;

void my_func (void *data,int which_func)
      switch (which_func) {
            case 1:
                  my_func_1 ((struct my_struct1 *)data);
            case 2:
                  my_func_2 ((struct my_struct2 *)data);
Why not have the second function, and just check to see if there were parameters passed on for the last two?  If they are both NULL, then do the first function, if not, then do the second.  Or have a third parameter which is a BOOL to toggle..

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now