In C++ How Is Function Overloading Typically Implemented

How to achieve function overloading in C?

There are few possibilities:

  1. printf style functions (type as an argument)
  2. opengl style functions (type in function name)
  3. c subset of c++ (if You can use a c++ compiler)

how an overloaded function is internally represented during compilation process in c++

In C, two functions with the same name in different blocks are named like so:

functionName.number

For example, you might have:

fun.2051

and

fun.2053

I figured this out by compiling a C program with the following command:

gcc -S -o test.asm test.c

I then opened up the assembly file and observed what gcc labeled the functions with.

Function overloading in C

It's using variable arguments. Those declarations appear only in the man page, as those 2 are the only ways you should call open(). The actual C function will be declared as e.g.

int open(const char *pathname,int flags,...);

With variable arguments, the arguments don't need to be the same type. printf is the obvious example of this.

In the case of open(), the first variable argument have to be mode_t if 'flags contain the O_CREAT flag because implementation of open() expects it to be mode_t (which behind the scenes is likely an unsigned int or unsigned long - but that has nothing to do with varargs)

Is overloading the only way to have default function arguments in C#?

Yes, that'd be best, except you'd omit the $s on the parameter names, as others have pointed out. For those interested in the rationale behind the lack of default parameter values, see @Giovanni Galbo's explanation.

Overloading by return type

No there isn't. You can't overload methods based on return type.

Overload resolution takes into account the function signature. A function signature is made up of:

  • function name
  • cv-qualifiers
  • parameter types

And here's the quote:

1.3.11 signature

the information about a function that participates in overload
resolution (13.3): its parameter-type-list (8.3.5) and, if the
function is a class member, the cv-qualifiers (if any) on the function
itself and the class in which the member function is declared. [...]

Options:

1) change the method name:

class My {
public:
int getInt(int);
char getChar(int);
};

2) out parameter:

class My {
public:
void get(int, int&);
void get(int, char&);
}

3) templates... overkill in this case.

Function overloading and function pointers

It depends on the context; otherwise it's ambiguous. See this example (modified except below):

void foo(int a) { }
void foo(int a, char b) { }

int main()
{
void (*functionPointer1)(int);
void (*functionPointer2)(int, char);
functionPointer1 = foo; // gets address of foo(int)
functionPointer2 = foo; // gets address of foo(int, char)
}

You can do this in many ways, but the #1 rule?

Avoid casts!

Otherwise you'll break type safety and probably shoot yourself in the foot either then or later.

(Issues can come up with calling conventions, random changes you don't notice, etc.)



Related Topics



Leave a reply



Submit