简体   繁体   English

我应该如何扩展标准功能,例如C语言中的字符串函数?

[英]How should I extend standard functionality, like string functions in C?

If I am writing my own string functions, the first instinct is to create a string.h header to contain the prototypes for those functions. 如果我正在编写自己的字符串函数,则第一个本能是创建一个string.h标头以包含这些函数的原型。 Of course, there is already a string.h file which is part of the standard set of libraries in C. This library has functions like strcat and strlen, etc. 当然,已经有一个string.h文件,它是C语言中标准库的一部分。该库具有strcat和strlen等功能。

If I am adding my own string handling functions, essentially extending what is already in the standard libraries, what is the right way to do that? 如果我要添加自己的字符串处理函数,本质上是扩展标准库中已有的内容,那么正确的方法是什么? Should I just create mystring.h and include string.h in it, or keep my stuff completely separate from the existing string.h? 我应该只创建mystring.h并在其中包含string.h,还是将我的东西与现有的string.h完全分开? If I don't want to use string.h can I just implement my own string.h, or is that a bad idea? 如果我不想使用string.h,我可以只实现自己的string.h,还是一个坏主意?

Note that I not re-writing strcat, I writing different functions that would ADDITIONAL to the existing ones, not replacements for them. 请注意,我没有重写strcat,而是编写了将对现有功能附加的不同功能,而不是对其的替代。

If you're creating a new set of functions, then you'll need to create your own header file with the declarations of those functions. 如果要创建一组新函数,则需要使用这些函数的声明来创建自己的头文件。

If on the other hand you're replacing some existing string functions, ie the names and parameters are the same, then you don't need your own header as the system header will work for those functions. 另一方面,如果要替换一些现有的字符串函数,即名称和参数相同,那么您就不需要自己的标头,因为系统标头将对那些函数起作用。 And since the standard library functions are weak symbols , you can link these replacement functions normally and they will override the system functions. 并且由于标准库函数是弱符号 ,因此您可以正常地链接这些替换函数,它们将覆盖系统函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM