Skip to main content
GameDev.net gamedev.net
🔒 Locked

private function in C

Started by SikCiv Sep 18, 2000 at 12:27 AM 3 replies 550+ views
Original Post
SikCiv
SikCiv
Two of my cpp files (actually coded in C) use the same function names, therefore conflict with eachover. How do I privatize functions in C ( I know how to do it in CPP using private: ) ive tried: private bool Flip() but I get a "missing ; before provate" compile error.
  Downloads:  ZeroOne Realm
Quantum
Quantum
uh, i dont think you can
unless you had a struct and had the functions members inside there..
why not just rename the funcctions?
Syzygy
Syzygy
There are two ways to do it. The first one, which I think works in both C and C++ is to prototype the function as static:

static bool Flip();
...
Flip() { ... }

The second only works with C++ compilers, but doesn''t require a class or anything. That is to wrap the function in an un-named namespace:

namespace {
bool Flip() { ... }
}


Hope this helps.
...Syzygy
ImmaGNUman
ImmaGNUman
If the two functions don''t know of eachother(i.e. they are in seperate files and the prototypes don''t find their way and see the other) its perfectly fine to have both functions having the same name, although its horrible style.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Syzygy
Syzygy
Acutally, it''s only safe to have two functions with the same prototype (same name and parameters) in separate files if those files are never going to be linked togeter. In C and C++, everything defined at global scope (not inside a function, class definition, namespace, etc.) is exported from the resulting object file, and is therefore available from outside of the file. This includes both functions and variables. Thus, if you have two C files, each of which define "int x;" at the global scope, when you link them together the compiler will complain because there are two variables with the same name but different locations. Likewise if you define two functions with the same prototype at the global scope (which is almost always how it''s done) in two different files. The linker has two functions with the same (decorated) name and it has no way to know which one to pick.

...Syzygy

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.