我发现在头文件"gobjects.h"中声明了这样的跟随函数:
(它来自斯坦福便携式图书馆.整个图书馆的源代码和头文件可以在这里找到:https://github.com/cs50/spl/tree/master/c)
/*
* Function: getWidth
* Usage: width = getWidth(gobj);
* ------------------------------
* Returns the width of this object, which is defined to be the width of
* the bounding box.
*/
double getWidthGObject(GObject gobj);
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是函数的名称是getWidthGObject,但在注释块中它指定了用法,就像名称一样getWidth.当我在自己的代码中调用此函数时,似乎两个名称都可以正常工作.只是为了澄清getWidth这个头文件中没有另一个名为声明的函数.
所以,我的问题是,为什么我们可以用两个不同的名称来调用这个函数,而其中较短的一个似乎从未被定义过?
getWidth在generic.h中:
#define getWidth(arg) getWidthGeneric(sizeof arg, arg)
Run Code Online (Sandbox Code Playgroud)
评论通常是过时的,但这看起来不像是问题.getWidthGeneric最终调用getWidthGObject在GENERIC.C.arg由以下构造... va_list:
double getWidthGeneric(int size, ...) {
...
type = getBlockType(arg);
if (endsWith(type, "GWindow")) {
return getWidthGWindow((GWindow) arg);
} else if (endsWith(type, "GObject")) {
return getWidthGObject((GObject) arg);
} else {
error("getWidth: Illegal argument type");
}
}
Run Code Online (Sandbox Code Playgroud)