在members之后添加注释
如果需要为file,struct,union,class或enmu的members添加注释,并且你需要将这些注释放在compound之内,可以考虑将注释块放在member的后面。这样就要在注释块中添加一个“<”标记。
下面是个例子:
int var; /*!< Detailed description after the member */
这个块在用来在在一个member之后添加一个注释块(Qt风格)
还有一种方式
int var; /**< Detailed description after the member */
或者是
int var; //!< Detailed description after the member
//!<
或者是
int var; ///< Detailed description after the member
///<
多数情况下只是需要在一个member后添加一个
brief描述,如下:
int var; //!< Brief description after the member
或者
int var; ///< Brief description after the member
注意这些块有相同的结构,不过是使用了<来表明在前或在后。
这里有个使用注释块的例子:
/*! A test class */
class Test
{
public:
/** An enum type.
* The documentation block cannot be put after the enum!
*/
enum EnumType
{
int EVal1, /**< enum value 1 */
int EVal2 /**< enum value 2 */
};
void member(); //!< a member function.
protected:
int value; /*!< an integer value */
};
Click here for the corresponding HTML documentation that is generated by doxygen.
警告:
这些块只能用来给members和parameters注释。不能用来给files,classes,unions,structs,groups,namespaces和enums添加注释。还有下一节中论述的structural commands在这种注释块中被忽略掉了
Documentation at other places
截止目前,我们谈到的注释块都是在file,class或namespace的declaration或definition之前还有是在member的前后。尽管这通常已经达到目的,但有时要将注释放在其他地方。为一个文件添加注释也是有必要的,但是没有“在文件之前”这种地方。Doxygen允许将注释块放在其他的任何地方(也有不允许的:例如函数体内或一个标准的C-style注释块中)。
这样作比较麻烦的是需要在注释块中添加一些structural命令。
Structual commands(像其他的commands)以“\”开头,或是一个“@”(使用JavaDoc风格),后面是命令名和一或多个参数。例如,如果你想给上面的Test类添加一个注释:
/*! \class Test
\brief A test class.
A more detailed class description.
*/
这里特殊命令“\class”用于指出该注释块中含有对Test类的注释。还有一些其他的structural命令:
·\struct to document a C-struct.
·\union to document a union.
·\enum to document an enumeration type.
·\fn to document a function.
·\var to document a variable or typedef or enum value.
·\def to document a #define.
·\file to document a file.
·\namespace to document a namespace.
·\package to document a Java package.
·\interface to document an IDL interface.
参看Special Commands获得更详细资料。
为了注释一个类中的member,首先要对该类作注释。同样的问题上升到namespace。要注释一个全局的function,typedef,enum或preprocessor定义,你需要首先定义包含它的文件(通常情况下是一个.h文件,因为这些文件包含可以export到其他源文件的信息)。
让我们重复一下,因为常常被忽视:要注释一个global objects(functions,typedefs, enum,macros等),必须注释它们所在的.h文件。换句话,至少要这样注释一下
/*! \file */
或这样一个
/** @file */
行在该文件中
这里用个名为structcmd.h的C头文件,讲解如何使用structal commands。
/*! \file structcmd.h
\brief A Documented file.
Details.
*/
/*! \def MAX(a,b)
\brief A macro that returns the maximum of \a a and \a b.
Details.
*/
/*! \var typedef unsigned int UINT32
\brief A type definition for a .
Details.
*/
/*! \var int errno
\brief Contains the last error code.
\warning Not thread safe!
*/
/*! \fn int open(const char *pathname,int flags)
\brief Opens a file descriptor.
\param pathname The name of the descriptor.
\param flags Opening flags.
*/
/*! \fn int close(int fd)
\brief Closes the file descriptor \a fd.
\param fd The descriptor to close.
*/
/*! \fn size_t write(int fd,const char *buf, size_t count)
\brief Writes \a count bytes from \a buf to the filedescriptor \a fd.
\param fd The descriptor to write to.
\param buf The data buffer to write.
\param count The number of bytes to write.
*/
/*! \fn int read(int fd,char *buf,size_t count)
\brief Read bytes from a file descriptor.
\param fd The descriptor to read from.
\param buf The buffer to read into.
\param count The number of bytes to read.
*/
#define MAX(a,b) (((a)>(b))?(a):(b))
typedef unsigned int UINT32;
int errno;
int open(const char *,int);
int close(int);
size_t write(int,const char *, size_t);
int read(int,char *,size_t);
Click here for the corresponding HTML documentation that is generated by doxygen.
Because each comment block in the example above contains a structural command, all the comment blocks could be moved to another location or input file (the source file for instance), without affecting the generated documentation. The disadvantage of this approach is that prototypes are duplicated, so all changes have to be made twice! Because of this you should first consider if this is really needed, and avoid structural commands if possible. I often receive examples that contain \fn command in comment blocks which are place in front of a function. This is clearly a case where the \fn command is redundant and will only lead to problems.
因为上例中每个注释块都包含一个结构化命令,所有注释快都可以放在其他位置或input文件(例如source file),都不会影像文档的生成。这种做法的缺点是原型被复制了,所以每次都要写两遍。因为如此,你应该首先考虑是否有必要,并尽可能避免structural commands。我常常收到含有这种文件,在一个函数前的注释中有“\fn”命令。这样是冗余的,导致容易出错。