如何处理C中具有相同名称的函数

时间:2022-03-07 09:36:48

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?

所以我有一个包含向量实现的头文件和源文件。我想使用向量来实现堆。所以我意识到我希望我的函数特定于类,所以我声明每个都是静态思考我可以做Vector :: get(int n,Vector :: Vector * vector)但显然::不是运算符C和静态只是让事情变得私密。任何人都可以帮助我理解如何进行正确的封装,而不是命名我所有的函数Heap_get或Vector_get?

1 个解决方案

#1


3  

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

C ++有名称空间和类说明符来区分这样的东西,但在C中,名称必须是唯一的。

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.

所以像vecGet()或heapCreate()这样的名字正是C开发人员这样做的方式。

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

现在你可以在C中做多态,但是对于你想要做的事情来说可能有些过分。

#1


3  

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

C ++有名称空间和类说明符来区分这样的东西,但在C中,名称必须是唯一的。

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.

所以像vecGet()或heapCreate()这样的名字正是C开发人员这样做的方式。

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

现在你可以在C中做多态,但是对于你想要做的事情来说可能有些过分。