Search code examples
cencapsulation

How to deal with functions with same name in C


So i have a header and source file containing an implementation for a vector. I want to use the vector to implement a heap. So I realized that I would want my functions to be specific to the class so I declared each one as static thinking I could do Vector::get(int n, Vector::Vector* vector) but apparently :: is not an operator in C and static just makes things private. Can anyone help me understand how I can make the correct encapsulation and not name all my get functions Heap_get or Vector_get?


Solution

  • C++ has namespaces and class specifiers to distinguish between things like that but, in C, names have to be unique.

    It's a time honored tradition to simply use a (generally short) prefix for your code and hope you never have to integrate code from someone else that has used the same prefix.

    So names like vecGet() or heapCreate() are exactly the way C developers do this.

    Now you can do polymorphism in C but it's probably overkill for what you're trying to do.