gpt4 book ai didi

c++ - 函数声明应该包含参数名称吗?

转载 作者:IT老高 更新时间:2023-10-28 21:38:51 30 4
gpt4 key购买 nike

您实际上认为更好的编码风格是什么:在 header 中声明函数/方法的参数名称,或者只在源文件中声明,因为两者都可以做?如果您真的考虑只在源文件中声明函数/方法的参数名称,那么您将如何声明默认值?

外部标题:

//One.hpp
#ifndef ONE_HPP
#define ONE_HPP
namespace eins {

/** \brief description
*
* \param one represents ....
* \param two represents ....
*/
void function(int,int);

}
#endif

// One.cpp
#include "One.hpp"

eins::function(int one,int two) {
//Do stuff//
}

内部标题:

//One.hpp
#ifndef ONE_HPP
#define ONE_HPP
namespace eins {

/** \brief description
*
* \param one represents ....
* \param two represents ....
*/
void function(int one,int two);

}
#endif

// One.cpp
#include "One.hpp"

eins::function(int one,int two) {
//Do stuff//
}

我个人的观点是第一种方式更好,因为用户实际上是被迫阅读评论/API,不能被误导只阅读参数名称。但我不确定这一点,实际上声明默认值会破坏我的风格,因为你必须在函数/方法的 header 声明中这样做。

最佳答案

虽然两者都很好并且被大量使用,但在头文件的声明中使用参数名称有一个明显的优势。

大多数文档系统(例如 doxygen)都会解析您的头文件并生成文档。例如,请看这里:http://libface.sourceforge.net/doc/html/classlibface_1_1_face.html

查看构造函数文档。

比较一下

Parameters:
x1 X coordinate of the top left corner of the face.
y1 Y coordinate of the top left corner of the face.
x2 X coordinate of the bottom right corner of the face.
y2 Y coordinate of the bottom right corner of the face.
id ID of the face. -1 not not known.
face A pointer to the IplImage with the image data.

还有这个

Parameters:
param1 X coordinate of the top left corner of the face.
param2 Y coordinate of the top left corner of the face.
param3 X coordinate of the bottom right corner of the face.
param4 Y coordinate of the bottom right corner of the face.
param5 ID of the face. -1 not not known.
param6 A pointer to the IplImage with the image data.

你明白了。 :)

关于c++ - 函数声明应该包含参数名称吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7891526/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com