tags:

views:

1365

answers:

8

I am having growing pains moving from Java to C. I have become used to having different methods with the same name, but which take different parameters. In C this creates problems?

Cell makeCell(int dim, int iterations, Cell parent);

Cell makeCell(Cell parent);

Is there some quick little work around for this problem, or should I just keep a stiff upper lip and call one of them _makeCell or makeCell2 or something equally ridiculous?

+10  A: 

In C, you do not have overloaded functions - functions with the same name but different types for the arguments (ignoring esoterica such as <tgmath.h> in C99).

The functions must be given different names.

Or use a different language that supports overloaded function names.

Jonathan Leffler
For example using a C++ compiler would work.
Georg
+2  A: 

Simply put, there is no method overloading in C, I think it would be possible to create preprocessor rules to rename the methods based on the number of arguments, but I have no idea how to do it, and it would give you far more work then simply using different names.

Vasco Fernandes
That is exactly how C++ came to be. The original implementation was as a preprocessor named cfront that translated the early dialect into valid C, which was then compiled with an ordinary C toolchain. Google will lead you to the source kit for cfront if your morbidly curious.
RBerteig
+1  A: 

Well there is no function overloading in C, but if you want or must use C, you can achieve something similar by creating a function that takes a variable number of arguments. You could start here for information on various techniques, some of which are fairly portable: http://en.wikipedia.org/wiki/Stdarg.h

Generally, though, I would caution against using this trick too extensively in C. The most common reason to use variable argument functions is to implement functions like printf(), not for function overloading kludges. But it's there if you want it.

Craig S
+5  A: 

There is no overloading in C as you understand it in Java or C++ (manually created overloading with function pointers notwithstanding but seriously, if you want object orientation, use C++, not C with kludges :-), so you should, as you suspect, call them different function names.

But don't call them _makeCell or makeCell2 since that's not descriptive. How about:

Cell makeCellFromDimensionAndIteration(int dim, int iterations, Cell parent);
Cell makeCell(Cell parent);

That first one could be shortened but make sure it still has meaning. makeCell2 will mean very little to someone reading the code (including yourself six months into the future).

paxdiablo
A: 

If you are coming from Java, you may find C++ much easier to get to grips with than C. And as the compiler you are using is almost certainly a C++ compiler, why not give it a try?

anon
+2  A: 

Welcome to C hell. There's one big flat namespace, there's no overloading, so you need conventions to help you invent and manage names. A good place to look for inspiration is Dave Hanson's C Interfaces and Implementations.

For your particular example I would suggest something along these lines:

Cell_T Cell_with_dim(int dim, int iterations, Cell_T parent);

Cell_T Cell_of_parent(Cell_T parent);

(Not super confident of these suggestions because I'm having a hard time guessing what the different overloadings are supposed to do.)

Norman Ramsey
+2  A: 

As others mentioned there's no way to create two functions with the same name, you can use something like:

Cell makeCell(Cell parent, int dim, int iterations);

for both cases and pass some "special value" to distinguish the two cases. For example:

cell = makeCell(par,-1,-1);

The other option I see is to use a variable number of arguments:

Cell makeCell(Cell parent, ...);

but then you have the problem of determining how many arguments have been passed and if you can't do it looking at "parent", you're basically back to the previous case as you have to use a "special value" to specify how many parameters are there.

If this is a sort of constructor (as the name suggests) I would rather have two functions:

Cell makeCell(Cell parent);
Cell setCell(Cell cell, int dim, int iterations);

one that creates a new empty Cell and the other that sets what needs to be set. Of course, it depends on the nature of "Cell" if this is a viable option for you.

Remo.D
+1, constructors with too many arguments are unreadable.
quinmars
A: 

It's a common problem, because you can't overload functions in C. Only the compiler can (as in tgmath.h). Having a look into tgmath.h of GCC already served my hack-o-meter for today. They found the right words i think

/* This is ugly but unless gcc gets appropriate builtins we have to do
 something like this. Don't ask how it works. */

People have invented different ways to solve it. OpenGL for example take a course where they include parameter types and count into the function name:

glVertex3f

To say the function takes 3 floats.

Johannes Schaub - litb